bug-bash
[Top][All Lists]
Advanced

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

Re: set -e ignored in subshell if part of command list


From: Chet Ramey
Subject: Re: set -e ignored in subshell if part of command list
Date: Wed, 13 Nov 2019 10:07:44 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.2.2

On 11/13/19 5:24 AM, Shaun Crampton wrote:

Bash Version: 5.0
Patch Level: 3
Release Status: release

Description:
        I was trying to get a function to return early if a command
fails by putting
        the body of the function in a subshell and using set -e inside
the subshell.
        If I run a subshell on its own, this works, but when I try to combine it
        into a larger program, the set -e gets ignored.

Repeat-By:
         Managed to boil it down to this smaller example:

         # On its own, subshell behaves as expected:
         $ ( set -ex; false; echo here )
         + false

         # In a list, behaviour changes, "echo here" gets executed:
         $ ( set -ex; false; echo here ) && echo there
         + false
         + echo here
         here
         there

The subshell command is part of an and-or list, so the -e is ignored for
that command:

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

(from https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_03)

The subshell inherits this state (being part of an and-or list) from its
parent.


        # If the subshell is executed in the background, it works
        $ ( set -e; false; echo here ) & pid=$!; wait $pid && echo there

In this command, the subshell is not part of an and-or list.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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