Let's compare these 2 scripts.
Script 1:
#!/bin/sh
set -e
foo() {
return 1
}
bar() {
foo
echo "this shouldn't be executed"
}
bar
Script 2:
#!/bin/sh
set -e
foo() {
return 1
}
bar() {
foo
echo "this shouldn't be executed"
}
if bar; then
echo "yes"
else
echo "no"
fi
Since there's set -e, I expect that if a function returns non-zero value, any function that calls it would also stop on that line and return the same value.
In other words, in the first script, foo return 1, therefore bar will also return 1 and the line echo "this shouldn't be executed" will not execute. In the end, the script will exit with code 1.
However, in the second script, suddenly, if I call bar inside if statement, it will not stop on the line, where it calls foo. It'd continue and echo "this shouldn't be executed".
I don't understand. What's so special about if statements? It seams that inside if statement condition, set -e doesn't have any effect.
Btw, similar unexpected behavior happens if I simply call bar || echo "this should execute". The echo won't happen and instead the line inside bar will execute echo "this shouldn't be executed".
set -eandif.set -e. A whole set of problems just goes away by following that guidance.set -eseems like a nice idea. It is often even heavily advised that people use it. But it's not, and you shouldn't. Any corporate standard that suggests using it should be heavily questioned.set -e(orset -o errexit, ortrap ERR) do what I expected?