More Bash answers

by Danny
Published 2 min read

Bash keeps running after a command fails. Learn what set -euo pipefail does, where it quietly does not apply, and how trap ERR reports the failing line.

The short answer

#

By default, Bash carries on to the next line when a command fails. Put set -euo pipefail near the top of your script so it stops on the first failure, treats unset variables as errors, and notices failures inside pipelines. Add an ERR trap if you want to know where it stopped.


_10
#!/usr/bin/env bash
_10
set -Eeuo pipefail
_10
trap 'echo "Error on line $LINENO: $BASH_COMMAND" >&2' ERR
_10
_10
cd /some/build/dir
_10
npm ci
_10
npm run build

Why it happens

#

Every command returns an exit status: 0 for success, anything else for failure. Bash records it in $? but does nothing else with it unless you ask. So a script like this happily deletes files in the wrong place when the cd fails:


_10
cd /tmp/my-build
_10
rm -rf ./*

The options change that behaviour:

  • set -e (errexit): exit as soon as a command returns a non-zero status.
  • set -u (nounset): treat expanding an unset variable as an error, instead of silently using an empty string.
  • set -o pipefail: a pipeline's status is that of the last command that failed, rather than always the last command. Without it, curl https://bad.example | tar xz only reports tar's status.
  • set -E (errtrace): make an ERR trap apply inside functions, command substitutions and subshells too.

How to fix it

#

Start scripts with strict mode


_10
#!/usr/bin/env bash
_10
set -Eeuo pipefail

This is a good default for build scripts, deploy scripts and anything that changes files. It turns silent failures into loud ones.

Report where it failed with trap ERR

The ERR trap runs whenever a command fails in a way that would trigger set -e. $LINENO and $BASH_COMMAND tell you where and what:


_10
on_error() {
_10
local status=$?
_10
echo "Failed with status $status at line $1: $BASH_COMMAND" >&2
_10
}
_10
trap 'on_error $LINENO' ERR

Use an EXIT trap for cleanup, since it runs however the script ends:


_10
tmp_dir="$(mktemp -d)"
_10
trap 'rm -rf -- "$tmp_dir"' EXIT

Allow failures on purpose

Some commands are expected to fail. Handle them explicitly so strict mode does not kill the script:


_10
# Ignore a failure
_10
rm -- old.log || true
_10
_10
# Branch on it
_10
if ! grep -q 'ready' status.txt; then
_10
echo "Not ready yet"
_10
fi
_10
_10
# Default for an optional variable under set -u
_10
port="${PORT:-3000}"

Common pitfalls

#

set -e has many exceptions, so do not treat it as a guarantee. The ones that catch people most often:

  • Conditions are exempt. Commands in an if or while test, or anywhere but the last part of an && or || list, do not trigger an exit. That includes everything inside a function you call from an if, so if my_func; then disables set -e for the whole of my_func.
  • local and export hide failures. In local out="$(cmd)" the status is that of local, which succeeds. Declare first, then assign: local out on one line and out="$(cmd)" on the next.
  • Command substitution does not inherit errexit by default in Bash. A failure in the middle of $(...) may not stop the script. Bash 4.4 and later offer shopt -s inherit_errexit to change this.
  • Arithmetic can exit unexpectedly. ((count++)) returns status 1 when count was 0, which ends the script. Use count=$((count + 1)) instead.
  • pipefail and early-closing readers. In some_command | head -n 1, head may exit first and some_command receives SIGPIPE, giving status 141 and stopping the script. Expect this when you cut output short on purpose.
  • set -u and arrays. On Bash older than 4.4 (including the 3.2 that ships with macOS), expanding an empty array with "${arr[@]}" counts as unset and errors.
  • Sourced scripts. set -e in a file you source changes the calling shell too, and can close your interactive terminal on the next failed command.

For anything critical, check important commands explicitly (cmd || exit 1) rather than relying only on set -e.

#

Was this resource helpful?