ERR trap fired in && compound inside a function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ERR trap fired in && compound inside a function

Yury Kartynnik
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
-DCONF_VENDOR='unknown'
-DLOCALEDIR='/home/kartynnik/.local/share/locale' -DPACKAGE='bash'
-DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib   -g -O2
-Wno-parentheses -Wno-format-security
uname output: Linux zedbook 4.10.0-40-generic #44~16.04.1-Ubuntu SMP
Thu Nov 9 15:37:44 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.4
Patch Level: 12
Release Status: release

Description:
        ERR trap gets fired when a non-last command in a && compound
command inside a function returns a non-zero code.
        This doesn't happen when the compound command is executed
directly from the shell.
        According to the trap documentation, non-last commands in &&
and || should not fire the ERR trap in any case.

Repeat-By:
        $ on_error() {
        > echo "Error $? in $@" >&2
        > }
        $ trap 'on_error $BASH_COMMAND' ERR
        $ false
        Error 1 in false
        $ # ^^^ works as intended
        $ false && true
        $ # ^^^ doesn't fire, complies with the docs
        $ f() {
        > false && true
        > }
        $ f
        Error 1 in false
        $ # ^^^ fires, doesn't comply with the docs

Reply | Threaded
Open this post in threaded view
|

Re: ERR trap fired in && compound inside a function

Chet Ramey
On 12/16/17 8:15 AM, Yury Kartynnik wrote:

> Bash Version: 4.4
> Patch Level: 12
> Release Status: release
>
> Description:
>         ERR trap gets fired when a non-last command in a && compound
> command inside a function returns a non-zero code.
>         This doesn't happen when the compound command is executed
> directly from the shell.
>         According to the trap documentation, non-last commands in &&
> and || should not fire the ERR trap in any case.
>
> Repeat-By:
>         $ on_error() {
>         > echo "Error $? in $@" >&2
>         > }
>         $ trap 'on_error $BASH_COMMAND' ERR
>         $ false
>         Error 1 in false
>         $ # ^^^ works as intended
>         $ false && true
>         $ # ^^^ doesn't fire, complies with the docs
>         $ f() {
>         > false && true
>         > }
>         $ f
>         Error 1 in false
>         $ # ^^^ fires, doesn't comply with the docs

The ERR trap fires because `f' returns 1, the return status of the last
command executed in the function body.

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

Reply | Threaded
Open this post in threaded view
|

Re: ERR trap fired in && compound inside a function

Yury Kartynnik
   Ah, pardon me, you're right. But I would expect $BASH_COMMAND inside
   the trap handler be 'f' in this case.
   18:01, 16 Ñ�нежнÑ� 2017, Chet Ramey <[hidden email]>:

     On 12/16/17 8:15 AM, Yury Kartynnik wrote:

     Â Bash Version: 4.4
     Â Patch Level: 12
     Â Release Status: release
     Â Description:
     Â Â Â Â Â Â Â Â Â ERR trap gets fired when a non-last command in a
     && compound
     Â command inside a function returns a non-zero code.
     Â Â Â Â Â Â Â Â Â This doesn't happen when the compound command is
     executed
     Â directly from the shell.
     Â Â Â Â Â Â Â Â Â According to the trap documentation, non-last
     commands in &&
     Â and || should not fire the ERR trap in any case.
     Â Repeat-By:
     Â Â Â Â Â Â Â Â Â $ on_error() {
     Â Â Â Â Â Â Â Â Â > echo "Error $? in $@" >&2
     Â Â Â Â Â Â Â Â Â > }
     Â Â Â Â Â Â Â Â Â $ trap 'on_error $BASH_COMMAND' ERR
     Â Â Â Â Â Â Â Â Â $ false
     Â Â Â Â Â Â Â Â Â Error 1 in false
     Â Â Â Â Â Â Â Â Â $ # ^^^ works as intended
     Â Â Â Â Â Â Â Â Â $ false && true
     Â Â Â Â Â Â Â Â Â $ # ^^^ doesn't fire, complies with the docs
     Â Â Â Â Â Â Â Â Â $ f() {
     Â Â Â Â Â Â Â Â Â > false && true
     Â Â Â Â Â Â Â Â Â > }
     Â Â Â Â Â Â Â Â Â $ f
     Â Â Â Â Â Â Â Â Â Error 1 in false
     Â Â Â Â Â Â Â Â Â $ # ^^^ fires, doesn't comply with the docs

     The ERR trap fires because `f' returns 1, the return status of the
     last
     command executed in the function body.
     --
     ``The lyf so short, the craft so long to lerne.'' - Chaucer
     Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ``Ars longa, vita brevis'' -
     Hippocrates
     Chet Ramey, UTech, CWRU [1][hidden email]
     [2]http://tiswww.cwru.edu/~chet/

   --
   Sent from Yandex.Mail for mobile

References

   1. mailto:[hidden email]
   2. http://tiswww.cwru.edu/~chet/