The commands that do not trigger exit
Bash ignores set -e for a command when the command is part of a condition that the script is testing on purpose. The main cases are the condition of an if or while, the commands on the left side of && or ||, and a command whose status is inverted with !. In each case the failure is information for the script, not an error.
set -e
if grep -q nothing /dev/null; then echo found; else echo "not found"; fi
false || echo "handled"
echo "still running"All three lines run without exiting. The grep fails inside the condition of the if, so the else branch runs. The false is on the left of ||, so the script continues to the echo, and the last line prints too. This is what you want, and it is why strict mode can be used with ordinary tests.
Arithmetic that returns a failure
The most surprising exit under set -e comes from arithmetic. The command (( expression )) returns status 1 when the value of the expression is zero, and status 0 otherwise. So a post-increment of a counter that starts at zero evaluates to zero, and the command fails, which stops the script.
set -e
count=0
(( count++ ))
echo "count=$count"The script ends after the arithmetic line, without printing the count. The value of count++ before the increment is zero, so the command reports failure. The fix is either to use the assignment form count=$((count+1)), which always returns status zero, or to add || true to the arithmetic command.
Prefer count=$((count + 1)) in strict scripts. An assignment that uses an arithmetic expansion does not report the arithmetic result as a status, so it cannot stop the script.
Functions called in a condition
When a function is called in a context where set -e is suspended, the suspension applies to everything the function does. So a function that contains a failing command keeps running after that command when it is called from an if, a while test, or the left side of ||. This is one of the hardest cases to notice, because the function looks correct when you read it alone.
set -e
step() { false; echo "step kept going"; }
if step; then echo "step succeeded"; fi
echo "end"The output includes step kept going, and the function returns the status of its last command, which is the echo. The if then sees success, so the script goes on to print step succeeded and end. Calling the function on its own, outside any condition, would stop the script at the false.
Command substitution and subshells
A command substitution runs in a subshell. By default in bash, that subshell does not inherit set -e, so a failure inside $(...) does not stop it. The substitution simply returns whatever text it printed. Recent bash versions offer shopt -s inherit_errexit, which turns the inheritance on, and scripts that need reliable failures should enable it.
set -e
value=$(false; echo "inside")
echo "value=$value"The output is value=inside. The failing false inside the substitution did not stop the subshell, and the assignment took the status of the substitution, which is the status of its last command. Writing the substitution as value=$(cmd) || exit 1 adds the check explicitly.
Assignments hide the status of a command
When you declare a variable and assign it in one step, such as local v=$(cmd), the status of the declaration is the status of local, not of cmd. That status is zero, so the failure is hidden. Declare the variable first, then assign it on a separate line, so that the failing command is the one bash checks.
Hands-on: test the exceptions
Run the block below. It checks the rules from this unit in a fresh bash with strict mode. Predict each line before reading the output. The lab asks you to write one script that survives the expected failure and one that does not.
bash -c 'set -e; count=0; (( count++ )) || true; echo "survived count=$count"'
bash -c 'set -e; false || echo handled; echo after'The first line prints survived count=1, because the OR suppresses the failing arithmetic. The second line prints handled and then after. Both scripts finish with status zero.