What does set -e mean in a bash script?
From help set
:
-e Exit immediately if a command exits with a non-zero status.
But it's considered bad practice by some (bash FAQ and irc freenode #bash FAQ authors). It's recommended to use:
trap 'do_something' ERR
to run do_something
function when errors occur.
I found this post while trying to figure out what the exit status was for a script that was aborted due to a set -e
. The answer didn't appear obvious to me; hence this answer. Basically, set -e
aborts the execution of a command (e.g. a shell script) and returns the exit status code of the command that failed (i.e. the inner script, not the outer script).
For example, suppose I have the shell script outer-test.sh
:
#!/bin/shset -e./inner-test.shexit 62;
The code for inner-test.sh
is:
#!/bin/shexit 26;
When I run outer-script.sh
from the command line, my outer script terminates with the exit code of the inner script:
$ ./outer-test.sh$ echo $?26