On Thu, Sep 25, 2025, at 12:32 PM, Stan Marsh wrote:
> Actually, as I understand it, this affects all so-called "compound commands"
> (I hope I am using the terminology correctly) - not just "select".
>
> Observe:
>
>  $ trap
> trap -- 'echo "Status: $?"' ERR
>  $ ls ksjdfh
> ls: cannot access 'ksjdfh': No such file or directory
> Status: 2
>  $ case 1 in 1) ls ksjdfh && :; esac
> ls: cannot access 'ksjdfh': No such file or directory
>  $ echo $?
> 2
>  $ case 1 in 1) ls ksjdfh && :; esac || echo Fail
> ls: cannot access 'ksjdfh': No such file or directory
> Fail
> $
>
> The point is that "case" retains the exit status of the last executed
> command (the failed "ls"), but does not itself trigger the trap.

This happens because the ls command fails during the part of an AND
list that suppresses the ERR trap.  Consequently, the trap is also
suppressed for the enclosing compound command.

https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/V3_chap02.html#tag_19_26:

        2.  The `-e' setting shall be ignored when executing [...]
            any command of an AND-OR list other than the last.

        3.  If the exit status of a compound command other than a
            subshell command was the result of a failure while `-e'
            was being ignored, then `-e' shall not apply to this
            command.

Compare:

        $ trap 'echo ERR: "$?"' ERR
        $ case x in *) ls foo && : ;; esac
        ls: foo: No such file or directory
        $ case x in *) ls foo ;; esac
        ls: foo: No such file or directory
        ERR: 1

-- 
vq

  • "set -e&quo... Stan Marsh
    • Re: "s... Lawrence Velázquez
    • Re: "s... Duncan Roe via Bug reports for the GNU Bourne Again SHell
      • Re: &qu... Lawrence Velázquez
        • Sou... Duncan Roe via Bug reports for the GNU Bourne Again SHell

Reply via email to