2

I use a system where scripts utilize set -e:

#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached

If I do this:

#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached

...the last line is reached. However, if I do this:

#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached

...the last line is reached even though ! true should be false.

More testing:

user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$ 

...so in these cases, the ! operator works correctly.

However, it does not work properly with set -e. Why?

Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.

10

Because that's what the standard says:

-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:

  1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word, or any command of an AND-OR list other than the last.

http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set

Bash's manual also points this out, with somewhat different wording.

I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.