bug-bash
[Top][All Lists]
Advanced

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

Re: using `set -e' in a subshell


From: Chet Ramey
Subject: Re: using `set -e' in a subshell
Date: Sun, 27 Mar 2005 17:15:13 -0500
User-agent: Mozilla Thunderbird 1.0.2 (Macintosh/20050317)

Jim Meyering wrote:
> Hi,
> 
> Is the following behavior intended?
> 
> I was surprised by the behavior of bash/zsh/ash/dash/pdksh,
> yet Solaris 5.9's /bin/sh does what I expected:
> 
>   # Solaris 5.9's /bin/sh
>   $ echo a; (set -e; false; echo foo) && echo b
>   a
> 
>   # Yet bash/zsh/ash do this:
>   $ echo a; (set -e; false; echo foo) && echo b
>   a
>   foo
>   b
> 
> The POSIX spec for set's -e option says the following,
> so it all comes down to how you interpret being ``part of
> an AND or OR list'':
> 
>   -e When this option is on, if a simple command fails for any of the
>   reasons listed in Section 2.8.1 (on page 46) or returns an exit status
>   value >0, and is not part of the compound list following a while,
>   until, or if keyword, and is not a part of an AND or OR list, and is
>   not a pipeline preceded by the ! reserved word, then the shell shall
>   immediately exit.
> 
> My take was to consider the subshell commands in isolation,
> where I expected the `set -e' to take effect.  There, the simple
> command `false' fails, so I expected that subshell to exit nonzero
> and not print `foo'.

Bash, and every other modern shell, interprets the subshell as being
part of the AND list, and disables the effect of `set -e'.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live...Laugh...Love
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

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