Searchable Bash cheat sheet with 100+ commands and idioms,
organized into the eleven sections you actually reach for at
2am. Variables: "${var}", defaults ${var:-x}, assign-if-unset
${var:=x}, error ${var:?msg}, strip ${var#…} ${var%…},
replace ${var//old/new}, length ${#var}, substring
${var:0:5}, case ${var^^} ${var,,}. Conditionals: [[ ]],
string and glob equality, =~ regex with BASH_REMATCH,
arithmetic (( )), -lt -gt -eq, file tests -f -d -e -L -s,
-z -n, && ||, case. Loops: for over lists and globs, C-style
for ((i=0;i<n;i++)), the safe while read -r line < file
pattern, until, select, break / continue, {1..10}. Functions:
name() and function forms, $1 ${10}, "$@" vs "$*", local,
return, capture with x=$(fn). Redirection: > >> 2> 2>&1 &>
< <<EOF <<'EOF' <<<, exec for the script itself, /dev/null.
Pipes: | |& & with $!, $(cmd), <(cmd), set -o pipefail, tee,
( ) subshell vs { } group. Strings: byte vs character length,
IFS-split into arrays, printf -v, glob prefix tests. Flow:
set -e -u -x, pipefail, trap EXIT for cleanup, trap INT TERM
for signals, exit N, $?, time. Script patterns: shebang,
getopts, hand-rolled long options, mktemp + trap, ANSI
colors with TTY detection, progress bar, BASH_SOURCE,
command -v, read -p, stderr logging. Arrays: declare -a /
-A, +=(), "${arr[@]}", "${#arr[@]}", "${!arr[@]}", unset,
mapfile. Pitfalls: always quote, cd may fail (use && not ;),
pipe-into-while runs in a subshell so counters die, set -e
misses failures in if / && / pipelines without pipefail,
unmatched glob becomes the literal pattern (nullglob), wc -l
counts newlines so the last unterminated line is missed,
[[ "5" > "10" ]] is true (string compare), $0 vs
${BASH_SOURCE[0]} in sourced scripts, macOS ships bash 3.2
(brew install bash for 4+), getopts can't do long options.
Every entry has bilingual text, a copy-ready example, and
a pitfall callout where it matters. Search filters across
command, description, pitfall, and example; one-click copy.