Interview Prep/Shell Scripting/Functions, exit codes, strict mode and traps

Explain `set -euo pipefail` line by line, and add the `-E` flag.

BeginnerConceptFunctions, exit codes, strict mode and traps
Answer
set -Eeuo pipefail
  • -e: exit when an unchecked command fails.
  • -u: treat expansion of an unset variable as an error (DB_HOST: unbound variable), catching typos.
  • -o pipefail: a pipeline's status is the last non-zero status of any stage, so curl ... | jq ... fails if curl fails.
  • -E: the ERR trap is inherited by functions, command substitutions and subshells.

Write defaults for optional variables with ${VAR:-} so -u does not trip on legitimately unset values.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Functions, exit codes, strict mode and traps