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: Koichi Murase
Subject: Re: How to print the actual command for BASH_COMMAND in trap?
Date: Sun, 13 Jun 2021 11:44:57 +0900

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



reply via email to

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