[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-bash] Why`trap DEBUG` has no effect if it is called inside a funct
From: |
Peng Yu |
Subject: |
[Help-bash] Why`trap DEBUG` has no effect if it is called inside a function? |
Date: |
Tue, 21 Apr 2015 20:44:01 -0500 |
Hi,
The following examples show that if `trap DEBUG` is called inside the
function resetdebugsig, it will not able to reset the DEBUG signal
outside the function. However, this problem does not exist for the
signal EXIT. Can anybody help me understand why there is a difference
between DEBUG and EXIT in this aspect?
~$ cat main.sh
#!/usr/bin/env bash
function resetdebugsig {
trap DEBUG
}
trap 'echo "bash>" "$BASH_COMMAND"' DEBUG
echo abc
resetdebugsig
echo xyz
~$ cat main1.sh
#!/usr/bin/env bash
trap 'echo "bash>" "$BASH_COMMAND"' DEBUG
echo abc
trap DEBUG
echo xyz
~$ ./main.sh
bash> echo abc
abc
bash> resetdebugsig
bash> echo xyz
xyz
~$ ./main1.sh
bash> echo abc
abc
bash> trap DEBUG
xyz
~/EXIT$ cat main.sh
#!/usr/bin/env bash
function resetexitsig {
trap EXIT
}
trap 'echo "$0 is exiting ..."' EXIT
resetexitsig
~/EXIT$ cat main1.sh
#!/usr/bin/env bash
trap 'echo "$0 is exiting ..."' EXIT
trap EXIT
~/EXIT$ ./main.sh
~/EXIT$ ./main1.sh
--
Regards,
Peng
- [Help-bash] Why`trap DEBUG` has no effect if it is called inside a function?,
Peng Yu <=