bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functions and set -e


From: Greg Schafer
Subject: Re: functions and set -e
Date: Wed, 30 Aug 2006 14:45:42 +1000
User-agent: Mutt/1.4.2.1i

On Wed, Aug 30, 2006 at 04:28:35AM +0000, Eric Blake wrote:
> > > > #!/bin/sh
> > > > set -e
> > > >
> > > > func () {
> > > >   false && echo false
> > > >   true && echo true
> > > >   false && echo false
> ^^^ Line 1
> 
> > > > }
> > > >
> > > > func
> ^^^ Line 2
> 
> > > >
> > > > echo done
> > > >
> > I'll take your word for it.. but I'm not totally convinced. At the very
> > least, this behavior is very confusing and apparently not documented. It's
> > just plain weird that the compound statement containing "false" causes the
> > function call to end up with a nonzero exit status only bacause the
> > statement appears *on the last line* of the function. The exact same
> > statement on the first line of the function behaves as expected.
> 
> As marked above, the line causing bash to exit is not line 1 (which
> was a compound statement), but line 2 (invoking a function by a
> simple statement).  Try rewriting line 2 as "func && echo false" to
> see the difference.

Thanks for trying to clarify it for me. Let me put it another way: If I
change Line 1 above to an if/then style statement instead of "&&" ie:

  if false; then echo false; fi

it works exactly like I'd expect instead of the counter-intuitive behavior
when using &&.

I still suspect something is amiss here. Maybe the bash docs just need to be
clarified.. Anyhoo, I suppose I should have tested with other shells before
reporting but I don't have any available at the moment..

Regards
Greg




reply via email to

[Prev in Thread] Current Thread [Next in Thread]