The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| Select Bash through PATH | #!/usr/bin/env bash | View examples |
| Enable stricter failure handling | set -Eeuo pipefail | View examples |
| Read an argument | input=${1:?usage: script INPUT} | View examples |
| Supply a default | mode=${MODE:-safe} | View examples |
| Preserve one argument | printf '%s\n' "$filename" | View examples |
| Create an array | files=("report one.txt" "report two.txt") | View examples |
| Pass every array member | command -- "${files[@]}" | View examples |
| Read a literal line | IFS= read -r line | View examples |
| Process a file line by line | while IFS= read -r line; do printf '%s\n' "$line"; done \
< input.txt | View examples |
| Test a regular file | if [[ -f $path ]]; then process "$path"; fi | View examples |
| Compare integers | if (( count > 10 )); then printf 'large\n'; fi | View examples |
| Match alternatives | case $mode in safe|fast) run "$mode" ;; *) exit 2 ;; \
esac | View examples |
| Define a function | log() { printf '%s\n' "$*" >&2; } | View examples |
| Keep function state local | local path=$1 | View examples |
| Parse short options | while getopts ':o:v' option; do case $option in o) \
output=$OPTARG ;; v) verbose=1 ;; esac; done | View examples |
| Remove parsed options | shift "$((OPTIND - 1))" | View examples |
| Create a temporary directory | workdir=$(mktemp -d) | View examples |
| Clean up on exit | trap 'rm -rf -- "$workdir"' EXIT | View examples |
| Check syntax | bash -n script.sh | View examples |
| Run static analysis | shellcheck script.sh | View examples |
Bash is effective for orchestrating commands and files, but its expansion and error rules demand precision. Quote expansions, keep data in arrays, validate arguments before changing state, and make cleanup explicit. Move to a general-purpose language when data structures or error recovery dominate the script.
Step by step
Detailed examples
Declare the interpreter and error policy
A shebang affects direct execution but not `bash script.sh`. env follows PATH, which is convenient across systems but may select an unintended interpreter in privileged or tightly controlled contexts. `set -Eeuo pipefail` catches many failures, yet errexit has context-dependent exceptions and is not a substitute for checking expected failures explicitly.
#!/usr/bin/env bash
set -Eeuo pipefail
printf 'bash version: %s\n' "$BASH_VERSION" Validate arguments and quote expansions
Parameter expansion can reject missing inputs or provide defaults before commands run. Double-quote variable and command substitutions unless splitting or globbing is specifically intended. Use `--` where a command supports it so a filename beginning with a dash is not parsed as an option.
#!/usr/bin/env bash
set -Eeuo pipefail
input=${1:?usage: inspect.sh INPUT}
mode=${MODE:-safe}
printf 'input=%s mode=%s\n' "$input" "$mode" Keep lists as arrays and lines as lines
A space-delimited string cannot reliably represent filenames because spaces, glob characters, and empty values are data. Arrays preserve argument boundaries; quote `${array[@]}` to pass each member separately. `IFS= read -r` avoids trimming whitespace and interpreting backslashes, and a final unterminated line may require an explicit fallback condition.
#!/usr/bin/env bash
set -Eeuo pipefail
files=("report one.txt" "report two.txt")
for file in "${files[@]}"; do
printf '<%s>\n' "$file"
done <report one.txt>
<report two.txt>Use Bash-native tests and small functions
`[[ ... ]]` avoids word splitting and pathname expansion and supports Bash pattern matching. `(( ... ))` is the natural numeric context, but an arithmetic result of zero has false command status and can interact with errexit. Functions share shell state; use local for temporary variables and return status for success or failure rather than printing encoded status values.
#!/usr/bin/env bash
set -Eeuo pipefail
describe() {
local path=$1
if [[ -f $path ]]; then
printf 'file: %s\n' "$path"
elif [[ -d $path ]]; then
printf 'directory: %s\n' "$path"
else
printf 'missing: %s\n' "$path" >&2
return 1
fi
}
describe . Parse short options with getopts
getopts is a Bash builtin for short options. A leading colon in the option specification enables silent error reporting so the script can produce its own usage messages. OPTARG contains an option argument and OPTIND identifies the next operand; shift only after parsing is complete.
#!/usr/bin/env bash
set -Eeuo pipefail
output=-
verbose=0
while getopts ':o:v' option; do
case $option in
o) output=$OPTARG ;;
v) verbose=1 ;;
:) printf 'option -%s needs a value\n' "$OPTARG" >&2; exit 2 ;;
?) printf 'unknown option: -%s\n' "$OPTARG" >&2; exit 2 ;;
esac
done
shift "$((OPTIND - 1))"
printf 'output=%s verbose=%s operands=%s\n' "$output" "$verbose" "$#" Initialize cleanup state before installing traps
mktemp creates a collision-resistant temporary path; check that it succeeds before registering cleanup. A single-quoted trap action expands variables when the trap runs, so initialize those variables first and keep them readonly when practical. Quote the path and use `--`; never derive a recursive-removal target from unchecked user input.
#!/usr/bin/env bash
set -Eeuo pipefail
workdir=$(mktemp -d)
readonly workdir
trap 'rm -rf -- "$workdir"' EXIT
printf 'temporary data\n' > "$workdir/result.txt"
wc -l < "$workdir/result.txt" Parse and lint before execution
`bash -n` catches syntax errors without running commands, but it cannot prove runtime behavior. ShellCheck identifies many expansion and portability hazards and explains each diagnostic. Add focused tests around argument parsing, failure cleanup, unusual filenames, empty input, and the external commands a script orchestrates.
bash -n script.sh
shellcheck --shell=bash script.sh Note: ShellCheck is a separate tool and may need installation through the operating system's package manager.
Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



