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_10set -Eeuo pipefail_10trap 'echo "Error on line $LINENO: $BASH_COMMAND" >&2' ERR_10_10cd /some/build/dir_10npm ci_10npm 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:
_10cd /tmp/my-build_10rm -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 xzonly reportstar's status.set -E(errtrace): make anERRtrap apply inside functions, command substitutions and subshells too.
How to fix it
#Start scripts with strict mode
_10#!/usr/bin/env bash_10set -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:
_10on_error() {_10 local status=$?_10 echo "Failed with status $status at line $1: $BASH_COMMAND" >&2_10}_10trap 'on_error $LINENO' ERR
Use an EXIT trap for cleanup, since it runs however the script ends:
_10tmp_dir="$(mktemp -d)"_10trap '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_10rm -- old.log || true_10_10# Branch on it_10if ! grep -q 'ready' status.txt; then_10 echo "Not ready yet"_10fi_10_10# Default for an optional variable under set -u_10port="${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
iforwhiletest, or anywhere but the last part of an&&or||list, do not trigger an exit. That includes everything inside a function you call from anif, soif my_func; thendisablesset -efor the whole ofmy_func. localandexporthide failures. Inlocal out="$(cmd)"the status is that oflocal, which succeeds. Declare first, then assign:local outon one line andout="$(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 offershopt -s inherit_errexitto change this. - Arithmetic can exit unexpectedly.
((count++))returns status 1 whencountwas0, which ends the script. Usecount=$((count + 1))instead. pipefailand early-closing readers. Insome_command | head -n 1,headmay exit first andsome_commandreceives SIGPIPE, giving status 141 and stopping the script. Expect this when you cut output short on purpose.set -uand 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 -ein a file yousourcechanges 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.
Related reading
#Was this resource helpful?