BASH

# switch
case "${FOO}" in
  abc) bar=123;;
  cde) bar=345;;
  efg) bar=567;;
esac
 
# while
while true
    do
    echo .
    sleep 10
done
 
# and
if [ "$foo" == "a" ] && [ "$bar" == "b" ]; then
 
 # or
if [ "$foo" == "x" ] || [ "$bar" == "y" ]; then
 
# both
if ( [ "$a" == "a" ] || [ "$a" == "b" ] ) && ( [ "$b" == "c" ] ); then
 
 
# enable color output systemwide for ls and grep
cat <<EOF>> /etc/profile.d/alias.sh
alias ls='ls --color=auto'
alias grep='grep --color=auto'
EOF
 
# get exitcode prevoius command
${PIPESTATUS[0]}
 
# redirect stdout AND stderr to file
COMMAND &> file.out
 
# sequence
echo {,my-}host{1,{3..5},9,10}
echo $(seq 1 4)
 
# random
$(((RANDOM%10)+1))
 
# for loop
START=1
END=10
for ((i=START; i<=END; i++)); do
    echo "i: $i"
done
 
# String regexp
if [[ "${MOTHERBOARD}" =~ H8DM(8|E)-2 ]] &&  [[ "${RAID}" =~ 9650SE-(4|8)LPML ]]; then
   echo "found"
fi
 
# replace commas with spaces
${foo//,/ }
 
# remove non digit
echo "${MY_VAR//[!0-9]/}"
 
# endless loop
while true; do
  YOUR_COMMAND
done
 
# read from standard input
IN=$(cat /dev/stdin); echo $IN
 
# process specified files in directory
for i in *.$1
do
	 echo "$i" "${i%$1}$2"
done
 
# ping all clients
NET=192.168.1
for i in {1..254}; do
   ping  -w 1 -c 1 ${NET}.$i > /dev/null
 
   if [ $? -eq 0 ]; then
      echo ${NET}.$i
   fi
done
 
# parameter expansion
echo ${variable:-default_value}
 
#
# shell variables
#
# internal field separator
IFS=$(echo -en "\n\b")
 
# get calling (parent) script name
CALLING_SCRIPT=$(ps -o command= $PPID)
SCRIPT=${CALLING_SCRIPT##*/}
 
# ssh interactive login
ssh foo@www.example.com -t "bash -ic 'bar -vvv'"
 
# uppercase
x=bar
echo ${x^}
Bar
echo ${x^^}
BAR
 
# lowecase
x=BAR
echo ${x,}
bAR
echo ${x,,}
bar

Variables

$@ - parameter

Shortcuts
https://skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/

test parameter count

if [ "$#" -eq 4 ]; then
    BMC_DNS=${1}
    BMC_IP=${2}
    HOST_DNS=${3}
    HOST_IP=${4}
elif [ "$#" -eq 2 ]; then
    BMC_DNS=${1}
    BMC_IP=$(dig +short ${BMC_DNS})
    HOST_DNS=${2}
    HOST_IP=$(dig +short ${HOST_DNS})
else
    echo "Usage: $0 BMC_DNS HOST_DNS"
    exit 1
fi
 
[ -z "${PROJECT}" ] && PROJECT=test

Run command on exit / interrupt

function cleanup {
    rm -f /tmp/$$/*.log
    rm -d /tmp/$$
}
trap cleanup SIGINT

Check

[ -L ~/bar ] || ln -s ~/foo ~/bar

Customize colors
https://misc.flogisoft.com/bash/tip_colors_and_formatting

Customize BASH prompt
https://wiki.archlinux.de/title/Bash-Prompt_anpassen

echo PS1="'"'\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@web1-dev:\w\$'"'" >> .bashrc

set

set -a # auto export variables
set -x # debug / show commands

Links
https://devhints.io/bash
http://misc.flogisoft.com/bash/tip_colors_and_formatting - Formatting
https://www.dartmouth.edu/~rc/classes/ksh/special-vars.html
http://wiki.bash-hackers.org/commands/classictest - BASH test commands
http://www.linuxtutorialblog.com/post/tutorial-conditions-in-bash-scripting-if-statements