[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
BASH_COMMAND does not expand correctly in subshells inside traps.
From: |
Emanuele Torre |
Subject: |
BASH_COMMAND does not expand correctly in subshells inside traps. |
Date: |
Thu, 4 Nov 2021 22:49:34 +0100 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -march=x86-64 -mtune=generic -O2 -pipe -fno-plt
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/bin'
-DSTANDARD_UTILS_PATH='/usr/bin' -DSYS_BASHRC='/etc/bash.bashrc'
-DSYS_BASH_LOGOUT='/etc/bash.bash_logout'
-DNON_INTERACTIVE_LOGIN_SHELLS
uname output: Linux t420 5.10.75-1-lts #1 SMP Wed, 20 Oct 2021
11:02:09 +0000 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 5.1
Patch Level: 8
Release Status: release
Description:
BASH_COMMAND does not expand to the expected value when used in a
subshell inside a trap.
I tried using the following DEBUG trap that i got from
http://mywiki.wooledge.org/BashGuide/Practices in bash-5.1:
trap '(read -p "[$BASH_SOURCE:$LINENO] $BASH_COMMAND?")' DEBUG
And noticed that $BASH_COMMAND always expanded to:
read -p "[...:...] $BASH_COMMAND?
where the ...:... is the expanded value of $BASH_SOURCE:$LINENO.
Then I tried to use the same trap in bash-4.4 and it worked as
expected.
Repeat-By:
$ cat test1
trap 'echo "$BASH_COMMAND"' DEBUG
echo hello
$ cat test2
trap '(echo "$BASH_COMMAND")' DEBUG
echo hey
bash
$ ./bash test1 # bash-4.4
echo hey
hey
$ ./bash test2 # bash-4.4
echo hey
hey
$ bash test1 # bash-5.1
echo hey
hey
$ bash test2 # bash-5.1
echo "$BASH_COMMAND"
hey
$ ./bash --version | head -1
GNU bash, version 4.4.0(3)-release (x86_64-unknown-linux-gnu)
$ bash --version | head -1
GNU bash, version 5.1.8(1)-release (x86_64-pc-linux-gnu)
- BASH_COMMAND does not expand correctly in subshells inside traps.,
Emanuele Torre <=