What is the meaning of set -o pipefail in Bash Script?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)