Reputation: 6643
What is the meaning of set -o pipefail
in the beginning of the shell script ?
Upvotes: 93
Views: 74005
Reputation: 241928
man bash
says
pipefail
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.
Where "pipeline" is
command1 | command2 | command3
Without pipefail
, the return value of a pipeline is the exit status of the last command in the pipeline, regardless of whether previous commands failed.
Example:
$ grep ^root /etc/passwd | cut -f 5 -d :
System Administrator
$ echo $?
0
$ grep ^nonexistant_user /etc/passwd | cut -f 5 -d :
$ echo $?
0
$ set -o pipefail
$ grep ^nonexistant_user /etc/passwd | cut -f 5 -d :
$ echo $?
1
Upvotes: 100