Strict mode
Enable checks
set -euo pipefail
Enables strict error and variable checks. -e exits on errors, -u rejects unset variables, and -o pipefail catches pipeline failures; see set.
Catch errors
set -Eeuo pipefail
trap 'echo "error on line $LINENO" >&2' ERR
Adds an error trap with line numbers. -E makes the ERR trap fire inside functions and subshells; see trap.
Safer word splitting
IFS=$'\n\t'
Restricts word splitting. IFS is set to newlines and tabs instead of every space; see IFS.
Related links
- Bash set builtin (gnu.org)
0 Comments for this cheatsheet. Write yours!