What is the meaning of set -o pipefail
in the beginning of the shell script ?
Asked
Active
Viewed 4.3k times
1 Answers
70
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
-
3is it advisable/necessary to use `pipefail` when using pipeline ? – User123 Jul 21 '21 at 07:24
-
2@User123 Depends on whether you want a pipeline to return non-zero when one of its components fail. – oguz ismail Jul 21 '21 at 07:42
-
7@User123: Depends on why you're piping, and what you're doing with it. If you're, say, using cat to get random bytes of data from /dev/urandom and sending them to xxd and head, then you probably don't want pipefail set: the cat command will never complete successfully. If you're using a helper function to make more useful logs off of a regular command, but you need the exit status of the regular command, then you do. – Jack Simth Aug 20 '21 at 20:28
-
7Yes, it depends. Wide-spread example is `grep` within a pipeline - it returns 1 if nothing found. But I wonder if (statistically) it is more common wish to detect failure by default rather than "swallowing" it by the 0 in the last command (especially in scripts). Personally, I enable it in all scripts, and work around by inspecting exit codes explicitly. I think enabling `pipefail` is a more sensible default. – uvsmtid Jan 28 '23 at 06:20
-
Hint: `set -euo pipefail` exits if sub-processes fail, or if an unset variable is attempted to be used, or if there's a pipe failure. – NeilG Aug 15 '23 at 09:24