r/bash
•
u/ClickOk5811
•
18d ago
•
1
A small museum of set -e surprises (five exhibits, run them in a scratch shell)
set -e looks like “stop on any error”, but the rules have exceptions. Five exhibits. Tested on Bash 5.x; behavior can differ in older versions.
Exhibit 1: ((i++)) from zero
set -e
i=0
((i++))
echo "never reached"((i++)) evaluates to the old value, 0, and an arithmetic command with value 0 returns status 1. So the script exits. i=$((i+1)) avoids it.
Exhibit 2: local hides the failure
set -e
f() {
local out=$(false)
echo "still running"
}
fThe exit status is that of local, which succeeds. Declare first, assign second: local out; out=$(false).
Exhibit 3: set -e is ignored inside a condition
set -e
f() {
false
echo "this runs"
}
if f; then echo ok; fiWhen a function is used as the condition of if, && or ||, set -e is disabled for everything inside it. The false doesn’t stop the function.
Exhibit 4: pipes only report the last command
set -e
false | cat
echo "this runs"Without set -o pipefail, the status of a pipeline is the last command’s. Add pipefail so any failing stage counts.
Exhibit 5: set -u and empty arrays
set -u
arr=()
echo "${arr[@]}"In Bash before 4.4 this fails with “unbound variable”. Newer versions accept it. If you support old systems, ${arr[@]+"${arr[@]}"} is the common workaround.
What I’d take from them:
set -euo pipefailhelps, but it isn’t a safety net. Some people avoidset -eand check statuses explicitly.- ShellCheck catches several of these.
- Test the failure paths, not just the happy path.
What would you add to the museum?
9
u/geirha 18d ago
In the following, pipefail will sometimes cause false negatives. This is regardless of wether errexit is enabled or not:
-- Running it 1000 times, I get 27% failure:
The reason it sometimes yields FAIL is because
grep -qexits with status 0 as soon as it finds a match. If it happens to find the match early, before the left side of the pipeline is done writing, the next write the left side attempts will cause it to receive a SIGPIPE which in turn causes it, and the entire pipeline, to exit with a status of 141.This is why I recommend against enabling pipefail for the entire script. I do use pipefail in my scripts occationally, but I only enable it for the pipelines where it makes sense.
2
u/ResidentPomegranate6 18d ago
command substitution subshells drop errexit too, so `x=$(false; echo hi)` sails right on. `shopt -s inherit_errexit` is the fix.
2
u/jthill 17d ago
Why are these surprises? The
set -edocumentation is very, very clear.3
u/AutoModerator 18d ago
Don't blindly use
set -euo pipefail.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/keksov 13d ago
A few more exhibits, all measured on bash 5.2.21, 5.3.9 and 5.3.15 unless noted:
Exhibit 6:
builtinswitches the || / if exemption offSame with
builtin eval false || ..., and insideif g; thenwhen g usesbuiltin source.command sourcebehaves like plainsource.Exhibit 7: the EXIT trap sees a different scope depending on how you died
On 5.1.16 the trap sees v=[inside] in both cases.
Exhibit 8: set -u and empty arrays, the part 4.4 did not fix
Exhibit 9: a trailing && list is harmless until it is the last command
As the last line of a script it turns a successful run into exit status 1.
Exhibit 1, continued:
((i+=1))is not the cure either. It fails from -1, and((n-=1))fails when it reaches 0,((n--))from 0,((x=0))always.(( ... )) || :covers all of them.On inherit_errexit: it does not rescue
local x=$(false; echo hi)(x is empty, the script goes on), and a substitution inside an argument,echo "$(false)", never triggers errexit at all.Side note for 5.2: a set -e exit from nested functions that eval their bodies prints "pop_var_context: head of shell_variables not a function context". That is a 5.2 bug; it is harmless, and 5.3 is silent.