help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why`trap DEBUG` has no effect if it is called inside a f


From: Peng Yu
Subject: Re: [Help-bash] Why`trap DEBUG` has no effect if it is called inside a function?
Date: Tue, 21 Apr 2015 22:53:22 -0500

> The DEBUG trap is not inherited (set to the default) and restored to its
> previous value unless the `functrace' option is set:

For `set -o functrace` or `set -T`, the following example shows that
commands inside functions inherit the same DEBUG trap as the
functions. But why 'f' is printed twice after `set -T`.

==================
~$ cat main.sh
#!/usr/bin/env bash

trap 'echo "bash> $BASH_COMMAND"' DEBUG

function f {
echo abc
}

f
set -T
f
~$ ./main.sh
bash> f
abc
bash> set -T
bash> f
bash> f
bash> echo abc
abc
==================

For `declare -t`, I don't see there is any difference. Do I use
`declare -t` correctly?

==================
~$ cat main.sh
#!/usr/bin/env bash

trap 'echo "bash> $BASH_COMMAND"' DEBUG

function f {
echo abc
}

f
declare -t f
f
~$ ./main.sh
bash> f
abc
bash> declare -t f
bash> f
abc
==================

The following example shows that I am able to set the signal for DEBUG
within a function so that the signal will be able outside the
function. Isn't it logical to allow the reset of the signal in the
similar way so that DEBUG signal outside the function will also be
reset?

==================
~$ cat main_inside1.sh
#!/usr/bin/env bash

function f {
trap 'echo "bash> $BASH_COMMAND"' DEBUG
}

function g {
trap DEBUG
}

f
trap -p DEBUG
g
trap -p DEBUG
~$ ./main_inside1.sh
bash> trap -p DEBUG
trap -- 'echo "bash> $BASH_COMMAND"' DEBUG
bash> g
bash> trap -p DEBUG
trap -- 'echo "bash> $BASH_COMMAND"' DEBUG
==================

Also the man page says "extdebug...  5. Function  tracing  is enabled:
 command substitution, shell functions, and subshells invoked with (
command ) inherit the DEBUG and RETURN traps." The following example
shows no difference on whether extdebug is enabled or not. Why
functions in `f` are not trapped with the command set for DEBUG at the
beginning of the script?

==================
~$ cat main.sh
#!/usr/bin/env bash

shopt -s extdebug
trap 'echo "bash> $BASH_COMMAND"' DEBUG
function f {
echo abc
echo xyz
}
f

~$ ./main.sh
bash> f
abc
xyz
==================

> "All other aspects of the  shell  execution  environment  are  identical
> between  a function and its caller with these exceptions: the DEBUG and
> RETURN traps (see the description  of  the  trap  builtin  under  SHELL
> BUILTIN  COMMANDS below) are not inherited unless the function has been
> given the trace attribute (see the description of the  declare  builtin
> below)  or  the -o functrace shell option has been enabled with the set
> builtin (in which case all  functions  inherit  the  DEBUG  and  RETURN
> traps),  and the ERR trap is not inherited unless the -o errtrace shell
> option has been enabled."

-- 
Regards,
Peng



reply via email to

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