[root@CentOs7_001 ~]# set -o allexport off braceexpand on emacs on errexit off errtrace off functrace off hashall on histexpand on history on ignoreeof off interactive-comments on keyword off monitor on noclobber off noexec off noglob off nolog off notify off nounset off onecmd off physical off pipefail off posix off privileged off verbose off vi off xtrace off
"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default."
设置了这个选项以后,包含管道命令的语句的返回值,会变成最后一个返回非零的管道命令的返回值
示例
1 2 3 4 5 6 7
#!/bin/bash
set -o pipefail ls a.txt | echo"hi" >/dev/null [[ $? -ne 0 ]] && exit 1 echo"test"
结果
1 2 3 4 5
[root@CentOs7_001 test-shell]# bash test02
ls: cannot access a.txt: No such file or directory
结论
set -o pipefail是去捕获最后一个返回非零的管道命令的返回值,也就是ls a.txt的返回值; 没有a.txt文件,返回值为非0,因此[[ $? -ne 0 ]]条件成立,脚本退出,不执行echo “test”