为什么bash errexit在函数调用中的表现不如预期?

在bash手册页中,它指出:

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
你开始引用 manual然后你切掉了解释这种行为的位,这是在下一句话中:

-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 a while or until keyword,part of the test in an if 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 !.

相关文章

用的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补全...