Search notes:

Bash - Immediately exit a script when it encounters an error: set -e

set -e causes a script immediately exits when it encounters an error. If one of the commands (or executables) that the script calls, returns a non-zero value, this is considered an error.
set -e is equivalent to set -o errexit.
The behaviour of set -e is demonstrated with the following four scripts (of which the latter three are called by the first one):

run.sh

./script-1.sh && ./script-2.sh && ./script-3.sh
Github repository about-Bash, path: /built-in/set/e/run.sh

script-1.sh

echo "\$-=$-"
set -e
echo "\$-=$-"

echo "This is script 1, \$-=$-"

ls script-1.sh
Github repository about-Bash, path: /built-in/set/e/script-1.sh

script-2.sh

echo "\$-=$-"
set -e
echo "\$-=$-"

echo "This is script 2, \$-=$-"

ls this-file-does-not-exist

echo "This echo won't be executed because the -e of ./run.sh stops the execution upon encountering an error"
Github repository about-Bash, path: /built-in/set/e/script-2.sh

script-3.sh

echo "\$-=$-"
set -e
echo "\$-=$-"

echo "This is script 3, \$-=$-"

ls script-3.sh
Github repository about-Bash, path: /built-in/set/e/script-3.sh

See also

Bash builtins
set
Apparently, it's better to use trap function ERR instead of set -e.

Index