bash – 源文件如何继承陷阱DEBUG?

运行以下代码段:
#!/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内部作为第二行,一切都按照需要工作(源文件中的命令也被捕获).

如何将此作为认行为,以避免为我需要提供的任何文件重复上述行?
换句话说:有没有机会告诉源文件继承DEBUG陷阱?

解决了主文件中的设置:
set -o functrace

来自BASH的人:

If set,any traps on DEBUG and RETURN are inherited by shell
functions,command substitutions,and commands executed in a subshell
environment. The DEBUG and RETURN traps are normally not inherited in
such cases.

作为’source’的shell函数,它们是继承的.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...