[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "trap" output from "if" statement redirected wrongly
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: "trap" output from "if" statement redirected wrongly |
Date: |
Thu, 14 Apr 2022 18:37:00 +0200 |
On Wed, Apr 13, 2022 at 02:58:30PM +0200, Frank Heckenbach wrote:
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat
> -Werror=format-security -Wall
> uname output: Linux mars 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1
> (2022-03-07) x86_64 GNU/Linux
> Machine Type: x86_64-pc-linux-gnu
>
> Bash Version: 5.1
> Patch Level: 16
> Release Status: release
>
> Description:
>
> This script writes "foo" to bar rather than stdout as I'd expect.
>
> It's triggered by the "if" statement (which doesn't even cause
> running in a subshell, so it's not that).
>
> #!/bin/bash
> set -e
> trap 'echo foo' 0
> #false > bar # "foo" written to stdout correctly
> if true; then false; else false; fi > bar # "foo" written to bar
The POSIX standard says to execute the EXIT trap in the same environment
as the last command executed in the script (that first "false"). I
believe that the current state of the standard streams is part of what
is included in "the environment".
The environment in which the shell executes a trap on EXIT shall
be identical to the environment immediately after the last
command executed before the trap on EXIT was taken.
(from
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap)
A trap on 0 is identical to a trap on EXIT.
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.