Issues with set -e and subshell

View: New views
2 Messages — Rating Filter:   Alert me  

Issues with set -e and subshell

by Mario TRENTINI :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Configuration Information [Automatically generated, do not change]:
Machine: i486
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i486' -                  DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i486-pc-linux-gnu' -                  DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -    DHAVE_CONFIG_H   -I.  -I../bash -I../bash/include -I../bash/lib   -g -O2 -Wall
uname output: Linux touchndie 2.6.18v1 #1 SMP Thu Sep 21 10:01:41 CEST 2006     i686 GNU/Linux
Machine Type: i486-pc-linux-gnu

Bash Version: 3.2
Patch Level: 39
Release Status: release

Description: Using set -e with subshell gives unexpected results

Repeat-By:

Hello,

First issue :

# The following works fine : end is displayed
( set -e ; echo Start ; false ; echo end )

# The following works in a different way : end is not displayed
( set -e ; echo Start ; false ; echo end ) || echo ERROR

# note that Bash 4 has the same behavior
# (GNU bash, version 4.0.28(1)-release)

Is it a bug or the expected behavior, if it is a bug, is there a workaround ?

Second issue :

# I expect the following to not display status but to exit the shell :
set -e ; ( exit 1 ) ; echo status $?

# I observed the expected behavior on Bash 4.

Is there a way for Bash 3 to have the same behavior as Bash 4 ?

Thanks in advance.

Best regards

Mario




Re: Issues with set -e and subshell

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mario TRENTINI wrote:

> Bash Version: 3.2
> Patch Level: 39
> Release Status: release
>
> Description: Using set -e with subshell gives unexpected results
>
> Repeat-By:
>
> Hello,
>
> First issue :
>
> # The following works fine : end is displayed
> ( set -e ; echo Start ; false ; echo end )
>
> # The following works in a different way : end is not displayed
> ( set -e ; echo Start ; false ; echo end ) || echo ERROR
>
> # note that Bash 4 has the same behavior
> # (GNU bash, version 4.0.28(1)-release)
>
> Is it a bug or the expected behavior, if it is a bug, is there a workaround ?

It is expected behavior.  The presence of || cancels set -e for the
command preceding the ||.

There are always workarounds.  You don't have to rely on set -e; you can
structure your code so that you manually check the exit status.

> Second issue :
>
> # I expect the following to not display status but to exit the shell :
> set -e ; ( exit 1 ) ; echo status $?
>
> # I observed the expected behavior on Bash 4.
>
> Is there a way for Bash 3 to have the same behavior as Bash 4 ?

Not using `set -e'.  That's the essential substance of the bash-3.x-
bash-4.x change: the commands whose failure cause the shell to exit
were expanded from simple commands to nearly every command, including
pipelines and compound commands.

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