set -e The set -e option instructs Bash to immediately exit if any command 1has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful.
In all widely used general-purpose programming languages, an unhandled runtime error - whether that's a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.
- By default, Bash does not do this. This default behavior is exactly what you want if you are using Bash on the command line
- you don't want a typo to log you out! But in a script, you really want the opposite.
- If one line in a script fails, but the last line succeeds, the whole script has a successful exit code. That makes it very easy to miss the error.
- Again, what you want when using Bash as your command-line shell and using it in scripts are at odds here. Being intolerant of errors is a lot better in scripts, and that's what
set -e
gives you.
Copied from: Bash strict mode