I have a shell script which checks for windows line endings.
(! git ls-files | xargs grep -I $'\r')
Your problem has nothing to do with
-e option of bash is a little tricky.
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command , exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a
untilkeyword, part of the test following the
elifreserved words, part of any command executed in a
||list except the command following the final
||, any command in a pipeline but the last, or if the command's return value is being inverted with
Let's look at a much simpler example:
$ cat exit_on_error_test.sh #!/bin/bash trap 'echo Interrupted because of an error' ERR set -e ! true echo Exit status: $? $ ./exit_on_error_test.sh Exit status: 1 $
So, even though the exit status of "
! true" was non-zero, the script was allowed to run to the end and output the value of the exit status. That's because we didn't have any failing command - the non-zero exit code was due to deliberate negation.
However, if we enclose "
! true" in parentheses we introduce a failing (compound) command.
$ cat exit_on_error_test.sh #!/bin/bash trap 'echo Interrupted because of an error' ERR set -e (! true) # This as a whole is now a failing (compound) command echo Exit status: $? $ ./exit_on_error_test.sh Interrupted because of an error $