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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Linux/sh...', 1745558422, '18.191.74.140', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Linux/shell/bash/built-in/set/e(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78