Frequently Asked Question
What does set -euo pipefail do and should I always use it?
The line set -euo pipefail enables three of bash's strictest error-handling options at
once. -e (errexit) tells the shell to exit immediately if any simple command returns a
non-zero exit status, instead of stumbling onward. -u (nounset) makes the shell error
out if you reference an unset variable, catching typos like $USRNAME` for `$USERNAME
before they silently become an empty string. -o pipefail changes how pipelines report
failure: by default, cmd1 | cmd2 exits with cmd2's status only, so a crashed cmd1
is invisible; with pipefail, the pipeline returns the first non-zero status in the chain.
Together these turn bash from a forgiving glue language into something that stops loudly
on the first problem, which is almost always what you want for an automation script. It
is not a silver bullet, set -e has notorious edge cases around conditional contexts,
and you sometimes have to disable it locally with cmd || true for commands whose
non-zero exits are expected, but it is a massive improvement over the default. Treat
it as the standard prelude for any script you intend to keep.