help-bash
[Top][All Lists]
Advanced

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

Why `trap - DEBUG` won't be effective if it is sourced?


From: Peng Yu
Subject: Why `trap - DEBUG` won't be effective if it is sourced?
Date: Thu, 8 Apr 2021 12:04:58 -0500

Hi,

See the following test case. `trap - DEBUG` won't be effective if it
is sourced. But trap <some_command> DEBUG will be effective if it is
sourced. Why is there such an asymmetry? Shouldn't `trap - DEBUG` be
also effective if it is sourced. Thanks.

$ ./main.sh
main.sh> echo 'Hello World1!'
Hello World1!
main.sh> echo 'Hello World2!' 1>&2
Hello World2!
main.sh> source trapundebug.sh
main.sh> echo 'Hello World1!'
Hello World1!
main.sh> echo 'Hello World2!' 1>&2
Hello World2!
main.sh> trap - DEBUG
Hello World1!
Hello World2!

==> ./main.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

source trapdebug.sh
echo 'Hello World1!'
echo 'Hello World2!' >&2
source trapundebug.sh
echo 'Hello World1!'
echo 'Hello World2!' >&2
trap - DEBUG
echo 'Hello World1!'
echo 'Hello World2!' >&2

==> ./trapdebug.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

trap -- 'if [[ $BASH_SOURCE ]]; then
    builtin printf "\e[33;1m%s\e[m\n" "${BASH_SOURCE##*/}> $BASH_COMMAND"
else
    builtin printf "\e[33;1m%s\e[m\n" "bash> $BASH_COMMAND"
fi >&2' DEBUG

==> ./trapundebug.sh <==
trap - DEBUG

-- 
Regards,
Peng



reply via email to

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