This quick blog post explains why and when you need to use “set -e” in bash shell scripts.
From the blog post:
There are abundant resources online trying to scare programmers away from using shell scripts. Most of them, if anything, succeed in convincing the reader to blindly put something that resembles
set -euo pipefail
at the top of their scripts. Let’s focus on the “-e” flag. What does this do? Well, here are descriptions of this flag from the first two results on Google for “writing safe bash scripts”:
“If a command fails, set -e will make the whole script exit, instead of just resuming on the next line” (https://sipb.mit.edu/doc/safe-shell/)
“This tells bash that it should exit the script if any statement returns a non-true return value.” (http://www.davidpashley.com/articles/writing-robust-shell-scripts/)
Unfortunately, this is bash we are talking about and the story is never that simple.
- Read more: When Bash Scripts Bite