Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2
uname output: Linux spectrex360 6.8.0-53-generic #55-Ubuntu SMP
PREEMPT_DYNAMIC Fri Jan 17 15:37:52 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.2
Patch Level: 37
Release Status: release

Description:

When using $BASH_COMMAND in an EXIT trap to tell the user what failed, I
came across a problem where errors in subshells would instead implicate the
command immediately before the subshell, instead of the subshell itself.

Repeat-By:

Execute this script
---
#!/bin/bash
set -e
trap 'echo "EXIT (rc: $?) with BASH_COMMAND=$BASH_COMMAND"' EXIT
if :; then (
    false
); fi
---
Receive this output:
---
EXIT (rc: 1) with BASH_COMMAND=:
---
Observe that the ':' command in the 'if' condition has been incorrectly
implicated in causing the exit.

Workaround:

Modify the above script to also include:
---
trap '' ERR
---
Observe the output changes to:
---
EXIT (rc: 1) with BASH_COMMAND=( false )
---

The presence of an effectively no-op ERR trap for some reason modifies the
population of $BASH_COMMAND to return a more correct value.

Reply via email to