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.
Alternative that exits when /bin/false
fails
Consider:
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…