运行以下代码段:
#!/bin/bash
function preexec ()
{
echo -e "\n-->preexec command: $BASH_COMMAND"
}
trap 'preexec' DEBUG
function testfunc ()
{
echo "testfunc called $1"
}
testfunc "main"
source "source.sh"
exit 0
source.sh 在哪里
#!/bin/bash
testfunc "source"
给出:
-->preexec command: testfunc "main"
testfunc called main
-->preexec command: source "source.sh"
testfunc called source
-->preexec command: exit 0
这意味着源文件中的每个命令都不会被 DEBUG 陷阱捕获。
实际上,如果我添加该行
trap 'preexec' DEBUG
在 source.sh 中作为第二行,一切都按需要进行(源文件中的命令也被捕获)。
如何将其设为默认行为以避免对我需要获取的任何文件重复上述行?换句话说:有没有机会告诉源文件继承调试陷阱?