[lug] How does bash "set -e" work?

Jeffrey S. Haemer jeffrey.haemer at gmail.com
Wed Jan 2 18:23:45 MST 2013


Lee,

The example may have been confusing.

Earlier posts in this thread showed that the " || " operator causes the
"set -e" in the subshell to be ignored. (If it weren't ignored, then a
"false" following it would cause the subshell to exit immediately with a
non-zero exit status.)

In the post you cited, Rob was asking a different question: Does "set -e"
have *any* effect in the subshell when the parent shell follows it with "
|| "?

His experiment answers "Yes. Looking at the environment shows that the
*same* change occurs with or without the ' || ' " .  In fact, there are
*no* differences in the environments with and without " || ".

In both, the errexit flag is set, yet in one, it causes the subshell to
exit, while in the other, the flag is ignored.

Rob deduces from this that the " || " persuades the parent shell to
instruct its subshell to ignore the flag, but that that instruction is not
passed through the environment. In other words, there's got to be some
other, out-of-band information channel to the child process.

I agree, and add that I think it's both a great puzzle, and a sweet set of
experiments that lead to Rob's solution.

I'll add another hypothesis, with an experiment to rule it out: Perhaps '
|| ' tells the subshell's *false* to return a zero exit status! Easy to
test:

$ ( set -e; false; echo $? ) || *any-other-command*
1

Nope, *false* still fails. Take out the '||', however, and there's no exit
status reported, because the *set -e* causes an immediate termination of
the subshell when it sees any failure.

(But does it have something to do with "false," which is a shell builtin?
You can answer this yourself with the command */bin/false*, a standalone
binary.)

I haven't looked at the bash source (or Googled, or even asked around, for
that matter), so I can't tell you more. Rob -- who stayed home on New
Year's Eve to read code -- may now be able to.

HTH!


On Wed, Jan 2, 2013 at 3:35 PM, Rob Nagler <nagler at bivio.biz> wrote:

> > My thinking is that for the example in the thread, all that is happening
> > is that the compound forms have a true return status. No need for
> side-channel
> > communication between the the parent and child shells (set, env and sort
> > returned true -- so -e doesn't matter).
>
> I don't think so:
>
> % (set -e; false) && echo true
> % (set -e; false; echo false; exit 1) && echo true
> false
> % (set -e; false; echo false) && echo true
> false
> true
>
> Rob
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: irc.hackingsociety.org port=6667 channel=#hackingsociety
>



-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell], http://seejeffrun.blogspot.com [blog],
http://www.youtube.com/user/goyishekop [vlog]
*פרייהייט? דאס איז יאַנג דינען וואָרט.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20130102/2f07075e/attachment.html>


More information about the LUG mailing list