Exit immediately if a pipeline (which may consist of a single simple command),
a subshell command enclosed in parentheses,or one of the commands executed as
part of a command list enclosed by braces…
所以我假设一个函数应该被视为括号括起来的命令列表.但是,如果对函数调用应用条件,则errexit不再保留在函数体内,并且在返回之前执行整个命令列表.即使您在为该子shell启用了errexit的函数内显式创建子shell,也会执行命令列表中的所有命令.这是一个演示该问题的简单示例:
function a() { b ; c ; d ; e ; } function ap() { { b ; c ; d ; e ; } ; } function as() { ( set -e ; b ; c ; d ; e ) ; } function b() { false ; } function c() { false ; } function d() { false ; } function e() { false ; }
( set -Eex ; a ) + a + b + false
( set -Eex ; ap ) + ap + b + false
( set -Eex ; as ) + as + set -e + b + false
现在,如果我对每个人都应用条件……
( set -Eex ; a || false ) + a + b + false + c + false + d + false + e + false + false
( set -Eex ; ap || false ) + ap + b + false + c + false + d + false + e + false + false
( set -Eex ; as ) + as + set -e + b + false + c + false + d + false + e + false + false
-e
Exit immediately if a pipeline,which may consist of a single simple command,a subshell command enclosed in parentheses,or one of the commands executed as part of a command list enclosed by braces returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following awhile
oruntil
keyword,part of the test in anif
statement,part of any command executed in a&&
or||
list except the command following the final&&
or||
,any command in a pipeline but the last,or if the command’s return status is being inverted with!
.