Set at the beginning of bash script:
set -Eeuxo pipefail
The -e option will cause a bash script to exit immediately when a command fails.
set -o pipefail
This particular option sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully.
This option causes the bash shell to treat unset variables as an error and exit immediately.
The -x option causes bash to print each command before executing it.
The documentation states that -E needs to be set if we want the ERR trap to be inherited by shell functions, command substitutions, and commands that are executed in a subshell environment. The ERR trap is normally not inherited in such cases.