help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] eval and set -e


From: Eduardo Bustamante
Subject: Re: [Help-bash] eval and set -e
Date: Mon, 15 May 2017 22:43:08 -0500

On Mon, May 15, 2017 at 8:04 PM, Peng Yu <address@hidden> wrote:
[...]
> I have the following code.
[...]
> ( eval 'set -e; false; echo xx' )
> echo xxx "$?"
> ( eval 'set -e; false; echo xx' ) || echo "$?"

Lets simplify. The above is equivalent to:

( set -e; false; echo xx )
echo xxx "$?"
( set -e; false; echo xx ) || echo "$?"

(i.e. the eval doesn't change the outcome).

Now, time to add some context from the relevant standard (emphasis
mine): 
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set

  -e

  When this option is on, when any command fails (for any of the reasons listed
  in Consequences of Shell Errors or by returning an exit status greater than
  zero), the shell immediately shall exit, as if by executing the exit special
  built-in utility with no arguments, with the following exceptions:

    1. The failure of any individual command in a multi-command pipeline shall
    not cause the shell to exit. Only the failure of the pipeline itself shall
    be considered.

    2. The -e setting shall be ignored when executing the compound list
    following the while, until, if, or elif reserved word, a pipeline beginning
    with the ! reserved word, or **any command of an AND-OR list other than the
    last**.

    3. If the exit status of a compound command other than a subshell command
    was the result of a failure while -e was being ignored, then -e shall not
    apply to this command.

So, in the first case (the "simple" subshell), errexit is NOT ignored,
and `false' triggers an error condition that exits the subshell with
the return code of 1.

In the second case (the AND-OR list), the list is composed of two
elements: a subshell and the echo, joined by an OR. According to
POSIX, errexit is ignored for every element of the list, other than
the last one, which means that errexit is ignored for the subshell,
the `false' does not trigger the subshell to exit, and the return
status is that of `echo xx', i.e. 0.



reply via email to

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