Bash Reference Manual:
trap
If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function. Refer to the description of the extdebug option to the shopt builtin (see The Shopt Builtin) for details of its effect on the DEBUG trap.
'via Blog this'This bit me today:
It seems that
- not only does the bash DEBUG trap not run for commands inside shell functions, but only once at the start
- which is not unreasonable as a default
- especially if shopt extdebug allowed you to "trap DEBUG into" shell functions (which I have so far not been able to make work)
- but "trap ... DEBUG" is actually disabled inside shell functions
- which means that you cannot have a shell function that formats the current trap status nicely
- or which tries to add a "trap ... DEBUG" handler to its caller
Unless I can get shopt extdebug to work as advertised - well, at least, as I tyhinik it is advertised - there is a loss of abstraction.
Kluge: pass in the old trap handler setup, compute new, apply outside function.
:-(
A shell session.
Mac: trap
trap -- 'shell_session_update' EXIT
It looks like Apple sets up a trap handler by default.
Mac: trap 'echo DEBUG' DEBUG
DEBUG
Setting up a trivial (but annoying) DEBUG trap handler.
Run once before any command is run.
Mac:
DEBUG
Or even when ENTER is pressed to get a new prompt
Mac: echo xx
DEBUG
xx
DEBUG
Run twice as set up above, once for the command,
once for the exit handler
Now trying to run trap in a function
Mac: trap
DEBUG
trap -- 'shell_session_update' EXIT
trap -- 'echo DEBUG' DEBUG
DEBUG
Mac: foo() {
> trap
> }
DEBUG
Mac: foo
DEBUG
trap -- 'shell_session_update' EXIT
DEBUG
The shell function can execute the trap builtin
but the trap DEBUG handler was disabled
No comments:
Post a Comment