bug-bash
[Top][All Lists]
Advanced

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

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


From: Chet Ramey
Subject: Re: ERR trap fired in && compound inside a function
Date: Sat, 16 Dec 2017 10:01:22 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.4.0

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    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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