Discussion:
[Help-bash] Why should [[ reset BASH_COMMAND in trap?
Peng Yu
2018-12-05 06:51:13 UTC
Permalink
Hi,

See the code below. [[ set BASH_COMMAND to [[ itself. But according to
the manual, should BASH_COMMAND remain as the command that causes the
trap (i.e., "(exit 3)", in this case). Do I misunderstand anything in
the manual or this is a bug of Bash?

BASH_COMMAND
The command currently being executed or about to be
executed, unless the shell
is executing a command as the result of a trap, in which
case it is the command
executing at the time of the trap.

$ ./main.sh
declare -- BASH_COMMAND="( exit 1 )"
declare -- BASH_COMMAND="( exit 2 )"
declare -- BASH_COMMAND="[[ -n y ]]"
declare -- BASH_COMMAND="( exit 4 )"
$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

trap 'true; declare -p BASH_COMMAND' ERR
(exit 1)
trap 'false; declare -p BASH_COMMAND' ERR
(exit 2)
trap '[[ x ]]; [[ y ]]; declare -p BASH_COMMAND' ERR
(exit 3)
trap '[ x ]; declare -p BASH_COMMAND' ERR
(exit 4)
--
Regards,
Peng
Chet Ramey
2018-12-05 14:10:11 UTC
Permalink
Post by Peng Yu
Hi,
See the code below. [[ set BASH_COMMAND to [[ itself. But according to
the manual, should BASH_COMMAND remain as the command that causes the
trap (i.e., "(exit 3)", in this case). Do I misunderstand anything in
the manual or this is a bug of Bash?
Thanks for the report. The conditional and arithmetic commands should not
be resetting BASH_COMMAND.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...