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

Lee Woodworth blug-mail at duboulder.com
Wed Jan 2 15:18:55 MST 2013


On 01/01/2013 12:32 PM, Jeffrey S. Haemer wrote:
> Lee,
>
> On Tue, Jan 1, 2013 at 12:07 PM, Lee Woodworth<blug-mail at duboulder.com>wrote:
>
>> At least with bash, subshells (without -e), command sequences and pipes
>> appear to return the status of the last command:
>>
>
> Ab-so-tively. I set "pipefail" in scripts where I want to catch every
> failure. When I remember to. :-(

I'll have to look into using that.

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).

Jeff H. 2012-12-30 12:48PM:

 > On Sun, Dec 30, 2012 at 12:00 PM, Rob Nagler <nagler at bivio.biz> wrote:

 > > The "||" isn't modifying stdout so how would it "know"
 > > except by some internal state passed in the fork.
 > >
 > Absolutely agree. Must be doing that.

 > *Set* and *env* track what the flag is being set to in the subshell.  Yet
 > something's being passed down in the environment, through the fork, from
 > the parent shell, that's telling the child "... but never mind about that."

Previously in the thread:

 > (set -e; { set; env; } | sort > y)
 > (set -e; { set; env; } | sort > x) || echo ok
 > diff x y

.......

> Semis are just a typing convenience that let you put more than one command
> on a line -- exactly like lots of other languages. I've been told that this
> feature was first introduced in Algol, by Edgar T. "Ned" Irons, who wanted
> to reduce the number of card boxes he had to carry around.
>
> Ned also invented syntax-directed compilation and the screen editor.
>
> If you're really academic, you'll already be thinking, "But in the shell,
> is it a statement terminator or a statement separator?" :-)

As for ; being a separator vs terminator:

function x { test "$1" == "OK" };
on the command line causes bash to prompt for more input.

while function x { test "$1" == "OK"; }
on the command line is complete and the function is defined.

So command-line completion is treating it as a separator.

In a script though:

   function x { test "$1" == "OK" }
   function x { test "$1" == "OK" };

are syntax errors, while

   function x { test "$1" == "OK"; }

is not.

> _______________________________________________
> 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




More information about the LUG mailing list