4

I can't find any documentation indicating that this is correct behavior. I've verified this behavior on bash 4.2.25 and 4.1.2. Those are the latest to which I have simple access. Much appreciated if anybody has an explanation for why the conditional changes the behavior of the preceding subshell.

# ( set -e; false ; echo bye )
# ( set -e; false ; echo bye ) || echo "failed"
bye
#

thanks, Brian

Brian Chrisman
  • 3,482
  • 1
  • 15
  • 16
  • Is this a kinda-sorta perversion of: "The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or ││ list except the command following the final && or ││, any command in a pipeline but the last, or if the command’s return value is being inverted with !." – Brian Chrisman May 28 '14 at 21:35
  • See my answer; that was my thinking as well. Incidentally, the same behavior exists in the forthcoming 4.3 version as well. – chepner May 28 '14 at 21:36
  • same behavior in a recent ksh too. – shellter May 28 '14 at 22:10
  • Somebody here pointed me to this: http://austingroupbugs.net/view.php?id=537 – Brian Chrisman May 29 '14 at 05:40
  • Sounds like it will be too difficult to fix and was rejected. Personally, I think it's fine to skip a set -e in a {} context, since there's no guaranteed subshell... but () gives me a false sense of protection from the external shell in this case. – Brian Chrisman May 29 '14 at 05:43

2 Answers2

2

I believe the emphasized portion of the description of the -e option in the bash 4.2 man page applies:

The shell does not exit if the command that fails is [...] part of any command executed in a && or || list [...]

The false command is part of the subshell which makes up the LHS of the || operator, so although it fails, the -e is ignored. Arguably, this is a bug, since false is not part of an || in the shell where set -e is actually set.

chepner
  • 497,756
  • 71
  • 530
  • 681
2

See the the following link

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.

Morad
  • 2,761
  • 21
  • 29