我有一个脚本如下:
function dbgtrap {
echo "badvar is $badvar"
}
badvar=0
function some_func {
badvar=1
badvar=2
badvar=3
}
set -o functrace
trap dbgtrap DEBUG
some_func
trap - DEBUG # turn off the DEBUG trap
获取脚本后,我得到了输出:
badvar is 0
badvar is 0
badvar is 0
badvar is 1
badvar is 2
badvar is 3
我期待的东西像:
badvar is 0
badvar is 1
badvar is 2
badvar is 3
输出的前两行来自哪里?
一旦设置了每个命令行,就会调用陷阱。如使用下面的 TRACE 输出所示:
IIRC shell 在处理结束时调用陷阱,因此在 badvar 值的函数调用输出之前有 3 行输出。