set -euxo pipefail in the first line after the shebang of your bash script and it will fail if any of your statements fail.
More information about what those set flags mean.
Actually to solve your question just
set -e could be enough in some cases, but the other additional flags are also nice to have in most cases (as in the example below which does not fail just with
set -e, you need
One example script below:
#!/usr/bin/env bash set -euxo pipefail echo $UNDECLARED VARIABLE echo "Script does not reach here"
If you run above script you will get this error:
UNDECLARED: unbound variable. However without
set -euxo pipefail script will exit with success error code (
echo $? will return zero because the last statement was executed without errors).
* Be the first to Make Comment