help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to print the actual command for BASH_COMMAND in trap?


From: Peng Yu
Subject: Re: How to print the actual command for BASH_COMMAND in trap?
Date: Sat, 12 Jun 2021 22:50:06 -0500

I don't get it. I still only get 'true' instead of 'false'. Could you
let me know what is wrong?

$ cat ./main.sh
#!/usr/bin/env bash
trap '{ :; } > /dev/null; false' EXIT
trap 'declare -p BASH_COMMAND' DEBUG
true
$ ./main.sh
declare -- BASH_COMMAND="true"
declare -- BASH_COMMAND="true"

On 6/12/21, Koichi Murase <myoga.murase@gmail.com> wrote:
> 2021年6月13日(日) 11:32 Peng Yu <pengyu.ut@gmail.com>:
>>
>> In the following script, 'true' is printed twice.
>>
>> My understanding is that, for trapped command ('false' in this),
>> BASH_COMMAND is still the last command before the trap ('true'). Is it
>> the case?
>
> BASH_COMMAND is reset to the last command outside the trap at the
> beginning of the trap handler but can change after the first command
> in the trap handler. So, this produces two invocations of DEBUG, the
> first with BASH_COMMAND=true and the second with BASH_COMMAND=false:
>
>   trap ':; false' EXIT
>
> If you don't want the output from the first one, you may redirect it
> to /dev/null.
>
>   trap '{ :; } >/dev/null; false' EXIT
>
> --
> Koichi
>


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]