Freddy Vulto wrote: > I'm having a hard time understanding how bash propagates `-e' to a > subshell. Considering the example script below: > - shouldn't C, D and E give no output? I know `-e' isn't in effect if > the test is part of a `&&', `||' or `!', but shouldn't `-e' be *in* > effect because the `&&', `||' or `!' are defined outside of the > subshell? > - shouldn't the lefthand and righthand subshells of the `&&' behave > the same in FGH, IJK and PQRS? Especially since LMNO works like > expected?
Bash executes user-specified subshells in a copy of the parent shell's "execution environment." For those subshells, the execution environment includes the value of the `errexit' option, and knowledge of whether or not the exit status should be ignored (that is, whether it's the RHS of a && or ||, whether it's in a pipeline, whether it's being executed asynchronously, and so on). Command substitution subshells do not inherit the errexit option, except when the shell is in Posix mode. Bash has behaved this way for a very long time; the check for Posix mode was added in bash-2.05. This is not well documented; it appears only in the section describing Posix mode. It will be documented more completely in bash-4.0. Command substitution subshells also don't inherit the flag saying whether or not to exit if a command fails; that flag is "attached" to the command of which it is a part. That is arguably a bug. The shell exits when a command on the RHS of a && or || list fails, and user subshells inherit this behavior, for compatibility: it's pretty much unanimous among the `major' shell implementors that the Posix standard contains a defect and that exiting is the proper behavior. This point came up a couple of months ago. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/