BASH

# switch
case "${FOO}" in
  abc)
    bar=123
    ;;
  *efg*|*hij*)
    bar=567
    ;;
  *)
    SYSADMIN=pko
    ;;
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
 
if [[ "$STR" =~ .*"$SUB".* ]]; then
  echo "It's there."
fi
 
# negate
if [[ ! $PATH =~ $temp ]]
 
# 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")
567
# 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
 
VAR_3="Reverse Cases"
echo ${VAR_3~}
echo ${VAR_3~~}

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

# cleanup color
tput init

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

Array
https://linuxconfig.org/how-to-use-arrays-in-bash-script

declare -a DHCP_PING_STATE
 
NETWORK_PING_STATES+=("${NETWORK_ID} ${NETWORK_NAME} ${ROUTER_PING_STATE} $(echo ${DHCP_PING_STATE[@]})")
 
for NETWORK_PING_STATE in "${NETWORK_PING_STATES[@]}"; do
    echo "${NETWORK_PING_STATE}"
done | column -t

Source .bashrc

ssh -t vm1 bash -ic '"pyenv install 3.5.10 -s"'

Debug output

DEBUG=1
((${DEBUG})) && echo foo

Online BASH script check
https://www.shellcheck.net/

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