Strict mode (common preamble)

set -euo pipefail
IFS=$'\n\t'

Variables & defaults

SyntaxMeaning
${VAR:-default}Use default if unset or empty
${VAR:=default}Assign default if unset or empty
${VAR:?msg}Exit with msg if unset or empty
${#VAR}String length
${VAR:offset:len}Substring

Tests

[[ -f path ]]      # regular file
[[ -d path ]]      # directory
[[ -z "$s" ]]      # empty string
[[ "$a" == "$b" ]] # string equality

Redirection

SyntaxPurpose
cmd >filestdout truncate
cmd >>filestdout append
cmd 2>&1 | tee logstderr merged, copy to file
cmd <filestdin from file
exec {fd}<fileOpen file descriptor

Loops & functions

for f in *.txt; do echo "$f"; done

while read -r line; do … done < file

foo() {
  local x=1
  echo "$1"
}