$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

When set -e Doesn't Exit

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:12

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.

BASH4 lines
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.

BASH4 lines
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.

Tip

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.

BASH4 lines
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.

BASH3 lines
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.

BASH2 lines
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.

set -e exceptions and trap cleanup

Visualizer

set -e stops a script when a command fails, but it skips a few contexts. A trap runs cleanup on exit, and the order of EXIT, ERR and INT decides what runs when.

1 / 6
contextset -e effect
plain failing commandexits the shell
if or while conditiondoes not exit
left side of && or ||does not exit; the command after the last operator still does
command negated with !does not exit
function body, when f runs in such a contextdoes not exit

set -e ends the shell when a command fails. The rows below are the exceptions; the first row is the default rule.

bash -c 'set -e; if false; then echo no; fi; echo "after if"; false || echo "after ||"; ! false; echo "after !"; f() { false; echo "inside f"; }; f || echo "f failed"; false; echo "never printed"'
Terminal lab · 2 steps · real Linux in your browser

Predict where set -e stops

Write one script that exits on an arithmetic failure and one that survives it with a guard.

01
Watch arithmetic stop a script
Create arith.sh in /workspace. It turns on set -e, sets count to 0, runs the post-increment (( count++ )), and then echoes survived: ``bash cat > arith.sh <<'EOF' #!/bin/bash set -e count=0 (( count++ )) echo survived EOF `` Run it and confirm that survived is not printed.
Show hint for step 1
The expression count++ has the value 0 before the increment, so the arithmetic command returns status 1.
02
Keep the script alive with a guard
Create guard.sh in /workspace. Keep set -e and the same counter, but add || true after the post-increment. Then echo survived count followed by the value of count: ``bash cat > guard.sh <<'EOF' #!/bin/bash set -e count=0 (( count++ )) || true echo "survived count=$count" EOF ``
Show hint for step 2
The OR with true makes the arithmetic failure expected. The counter is 1 after the increment, so the output shows count=1.
Check yourself Question 1 of 4
Concept Check Commands ignored by set -e recall

Under set -e, which of these failures does bash deliberately not treat as an exit?

Recap & Key Takeaways

set -e ignores failures in if and while tests, on the left of && and ||, and in commands inverted with !, because those failures are expected checks.
An arithmetic command such as (( count++ )) returns 1 when its value is zero, so it can stop a strict script; use count=$((count + 1)) instead.
A function called from a condition runs with set -e suspended, and command substitutions do not inherit set -e unless inherit_errexit is set.
Declaring and assigning in one step, as in local v=$(cmd), hides the status of cmd, so assign on a separate line.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue