According to this accepted answer using the set -e
builtin should suffice for a bash script to exit on the first error. Yet, the following script:
To simplify EtanReisner's detailed answer, set -e
only exits on an 'uncaught' error. In your case:
echo "about to fail" && /bin/false && echo "foo"
The failing code, /bin/false
, is followed by &&
which tests its exit code. Since &&
tests the exit code, the assumption is that the programmer knew what he was doing and anticipated that this command might fail. Ergo, the script does not exit.
By contrast, consider:
echo "about to fail" && /bin/false
The program does not test or branch on the exit code of /bin/false
. So, when /bin/false
fails, set -e
will cause the script to exit.
/bin/false
failsConsider:
set -e
echo "about to fail" && /bin/false ; echo "foo"
This version will exit if /bin/false
fails. As in the case where &&
was used, the final statement echo "foo"
would therefore only be executed if /bin/false
were to succeed.