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


Groups > gnu.bash.bug > #15670

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

From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: set -e ignored in subshell if part of command list
Date 2019-11-13 10:07 -0500
Message-ID <mailman.1343.1573657685.13325.bug-bash@gnu.org> (permalink)
References <CAMhR0U1fzAvXMVZacEYf9yAPwgq_6Gz5C-99dj6xt=9pQMtX8Q@mail.gmail.com> <13040a55-507d-e072-e827-be7c33be968f@case.edu>

Show all headers | View raw


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/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: set -e ignored in subshell if part of command list Chet Ramey <chet.ramey@case.edu> - 2019-11-13 10:07 -0500

csiph-web