Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #4882

Re: [zsh] How to mimic errexit within subshell (or function)?

From Stephane Chazelas <stephane.chazelas@gmail.com>
Newsgroups comp.unix.shell
Subject Re: [zsh] How to mimic errexit within subshell (or function)?
Date 2012-05-02 20:27 +0100
Organization A noiseless patient Spider
Message-ID <20120502192715.GG10446@chaz.gmail.com> (permalink)
References <jnpk36$jqs$1@reader1.panix.com> <slrnjq235r.qt6.vaeth@lounge.imp.fu-berlin.de> <jns0pa$apr$1@reader1.panix.com>

Show all headers | View raw


2012-05-02 19:09:30 +0000, kj:
> 
> First of all, thanks for all the replies!
> 
> In <slrnjq235r.qt6.vaeth@lounge.imp.fu-berlin.de> Martin Vaeth
> <vaeth@mathematik.uni-wuerzburg.de> writes:
> 
> >kj <no.email@please.post> wrote:
> >>
> >> despite the set -e (errexit), the subshell does
> >> not exit on error
> 
> >Here it does (in script and also interactive):
> 
> ># ( set -e; printf a; false; printf b ); echo $?
> >a1
> 
> Thanks.  But now I'm **really** confused.  I don't why the first
> subshell below (i.e. your version) terminates upon error but the
> second one (mine) doesn't:
> 
> % ( set -e; printf a; false; printf b ); echo $?
> a1
> % ( set -e; printf a; false; printf b ) || echo $?
> ab% 
> 
> Actually, I don't understand why mine doesn't work, period, since
> I can't find anything in the zsh docs saying that it shouldn't...
> Any pointers to the place in the zsh docs where this is explained
> would be much appreciated.  (I hope that *some day* I'll learn to
> find my way around the zsh documentation, but I'm nowhere near that
> point yet.)
[...]

It's not specific to zsh.

The effect of "set -e" is cancelled when on the left hand side
of || or && or in and "if", "while" or "until". Basically
everywhere where the exit status is checked. In other words,
when a non-zero exit status is checked as part of a condition.

false

exits the shell. (false fails)

but not

false || true
(false is taken as a condition upon which to execute true or
not)

A side effect is that "set -e" is cancelled for the whole of the
subshell in:

(false; echo still here) || true

It's a bit confusing but that's how all the shells behave.

"|| true" or "|| : ignore errors" is the idiom to say you don't
care about the exit status when "set -e" is in effect.

set -e
command-that-may-fail-but-we-dont-care || : ignore error

-- 
Stephane

Back to comp.unix.shell | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[zsh] How to mimic errexit within subshell (or function)? kj <no.email@please.post> - 2012-05-01 21:20 +0000
  Re: [zsh] How to mimic errexit within subshell (or function)? Stephane Chazelas <stephane.chazelas@gmail.com> - 2012-05-02 07:34 +0100
    Re: [zsh] How to mimic errexit within subshell (or function)? Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2012-05-02 13:51 +0100
      Re: [zsh] How to mimic errexit within subshell (or function)? Stephane Chazelas <stephane.chazelas@gmail.com> - 2012-05-02 14:29 +0100
    Re: [zsh] How to mimic errexit within subshell (or function)? kj <no.email@please.post> - 2012-05-02 19:33 +0000
      Re: [zsh] How to mimic errexit within subshell (or function)? Stephane Chazelas <stephane.chazelas@gmail.com> - 2012-05-02 20:53 +0100
  Re: [zsh] How to mimic errexit within subshell (or function)? Martin Vaeth <vaeth@mathematik.uni-wuerzburg.de> - 2012-05-02 10:30 +0000
    Re: [zsh] How to mimic errexit within subshell (or function)? kj <no.email@please.post> - 2012-05-02 19:09 +0000
      Re: [zsh] How to mimic errexit within subshell (or function)? kj <no.email@please.post> - 2012-05-02 19:12 +0000
      Re: [zsh] How to mimic errexit within subshell (or function)? Stephane Chazelas <stephane.chazelas@gmail.com> - 2012-05-02 20:27 +0100
        Re: [zsh] How to mimic errexit within subshell (or function)? kj <no.email@please.post> - 2012-05-03 18:20 +0000

csiph-web