set -o errtrace
shell脚本中的上述语句将执行什么? 特别是-o
命令?
从手册:
errtrace Same as -E. -E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a sub‐ shell environment. The ERR trap is normally not inher‐ ited in such cases.
当启用errtrace
时,当在函数或子shell中发生错误(一个返回非零代码的命令)时,也会触发ERR陷阱。 换句话说,我们可以说,除非启用了errtrace
否则函数或子shell的上下文不会继承ERR陷阱。
#!/bin/bash set -o errtrace function x { echo x start false echo x end } function y { echo y start false echo y end } trap 'echo "Error occurred on $FUNCNAME."' ERR x y false true
输出:
x start Error occurred on x. x end y start Error occurred on y. y end Error occurred on .
当errtrace
未启用时:
x start x end y start y end Error occurred on .