How to Set & Use Pipefail in Bash [Explained Guide]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Successful working with piped commands in Bash demands the checking of exit status– especially when commands within the pipeline encounter failures. This allows for accurate management of these failures with effective flow control in complex pipelines (piped commands). As a result, it makes “pipefail”, a pivotal concept to learn. So, this article discusses how to control pipeline behavior using the option pipefail in Bash. It mentions both terminal and Bash script-specific executions to provide a comprehensive overview of “pipefail” and its practical implications.

What is “pipefail” in Bash?

In Bash, pipefail is a setting that enables the exiting of a pipeline with a non-zero exit code if any command in a pipeline fails. it‘s an option used with the set command which ensures that if any command in the pipeline fails, its exit code will be used as the exit code of the whole pipeline. Otherwise, it’s zero (0) if all the commands in the pipeline execute successfully. The piping of commands generally introduces complexity. So, checking the exit status of any failed command provides users with robust error handling where “pipefail” comes into play. However, before using “pipefail” option, it must be enabled using the set command since it’s disabled by default.

To enable “pipefail” in Bash, open the terminal and run the following command:

set -o pipefail

Now, to check if pipefail is set (enabled), execute the command below:

set | grep "pipefail"

set and check pipefailThe image above states that “pipefail” is set successfully.

Anyway, it’s a good practice to disable “pipefail” after pipeline execution running the below command:

set +o pipefail

Note: By default, Bash considers the exit code of the last command as the exit status of the whole pipeline. However, when the pipefail option is set, the exit code of a pipeline is determined by the last command to exit with a non-zero status code. This allows users robust error handling with improved error reporting in pipelines.

How Does Bash “pipefail” Work?

To know how the “pipefail” option works in Bash, open the terminal and run the following pipeline:

cat file.txt | wc -w

Then, run this command to check the exit code of the executed pipeline:

echo $?

basic pipeline exit codeHere, the file.txt does not exist and thus the cat command fails in printing the contents of “file.txt” in the terminal. Still, the exit code of the pipeline is 0 since the last command wc successfully works.

Now, run the above command cat file.txt | wc -w again but first enable the “pipefail” option with the set command:

set -o pipefail

pipefail is set and controls exit codeSo, you see when the option “pipefail” is set, it returns the exit code 1 instead of 0. This is because it affects the default behavior of piped commands (returning the exit code of the last successful command). Rather, it ensures that the pipeline reflects the exit status of the failing command cat file.txt.

How to Use Bash “pipefail” in Terminal?

To use the Bash “pipefail” in the terminal, enable it using the command set -o pipefail (as shown above) before executing the pipeline. Finally, run piped commands like the following example:

cat file.txt | wc -w | echo "pipeline ends"

pipefail in terminalHere, “pipefail” ensures that the exit code returned for the pipeline is 127 (exit code of the last executed command ECHO that fails).

“pipefail” With One-line Execution in Bash

Additionally, you can enable the pipefail option with the set command and execute piped commands in a single line. Just use the logical AND (&&) operator in between to ensure that the pipeline runs only if “pipefail” is set. Here’s an example below in the terminal:

set -o pipefail && Echo "hello" | wc -w

pipefail with one line execution

Using “pipefail” in Bash Scripts

Apart from using it directly in the terminal, it is possible to use the “pipefail” option within Bash scripts that deal with pipelines. This allows effective handling of errors and deciding further actions especially working with complex pipelines.

Here’s an example of “pipefail” in Bash scripts to see how it influences exit status:

#!/bin/bash

# enable pipefail
set -o pipefail

#the pipeline to execute
echo "RAW is war1 and war2" | GREP "war" | echo "pipeline finishes"

#the exit code of the pipeline
echo "the exit code of pipeline: $?"
EXPLANATION

The script above first enables the pipefail option using the command set -o pipefail. Then it executes a pipeline of 3 commands. Since the 2nd command GREP (instead of grep) fails, pipefail influences the exit code of the entire pipeline to be the code of the “GREP” command that failed. This is true even if the “echo” command (last command) succeeds.

pipefail in scripts

The exit code of the entire pipeline is 127 even though the last command is successfully executed.

Conclusion

This article discusses the “pipefail” option in Bash which is used with the “set” command. It shows how “pipefail” affects the pipeline behavior in returning the exit code for denoting error or success. Specifically, it demonstrates how errors are handled within the Bash piped commands using this “pipefail” option both in the terminal and using Bash scripts.

Frequently Asked Questions

Is “pipefail” a command in Bash?

No, it’s not. In Bash, “pipefail” is an option that is enabled or even disabled using the set command. For example, running the command set -o pipefail enables the option “pipefail”. As a result, it ensures that the exit code of the pipeline is not the code of the last executed command. Rather, the exit code will be the code of the last command in the pipeline that fails.

What is set -o pipefail in Bash?

The set -o pipefail is a command in Bash that enables the “pipefail” setting. By enabling “pipefail”, the command makes sure that a pipeline returns an exit status of the last command with a non-zero exit code leveraging error handling in pipelines.

Why is it useful to use “pipefail” in Bash scripts?

Using the option “pipefail” is useful in Bash because it ensures that a pipeline fails with the exit code of the last executed command that fails. This allows for effective error detection and debugging when working with a chain of commands (pipelines) including robust process management.

How to unset Bash “pipefail”?

To unset Bash “pipefail”, open the terminal and type the command set +o pipefail. Upon running the command pressing ENTER, it will disable the pipefail option. As a result, the default behavior of the pipeline to exit with the code of the last executed command will return.

How does “pipefail” affect the exit code of a pipeline in Bash?

The “pipefail” affects the exit code of a pipeline by making it exit with the code of the last command that fails. In general, the exit code of a pipeline is the exit code of its last executed command. For example, after running the piped commands echo "hello" | GREP "hello" | echo "ends", the exit status will be 0 since the last command succeeds by printing the string “ends”, even though GREP fails.  On the other hand, if pipefail is set, then the exit code of the pipeline would become 127 since the command that fails is “GREP” (not grep).

How do I set “pipefail” in Bash scripts?

To set “pipefail” in Bash scripts, add the line of command set -o pipefail just before adding the piped commands you want to execute. Such a setting will ensure if any command in the pipeline (used in the script) fails, the script will exit with the non-zero status of the failed command.

What does the command set -euxo pipefail do in Bash?

The command set -euxo pipefail stands for the commands set -e, set -u, set -x, and set -o pipefail. These options with the set command cause Bash scripts to exit based on different error conditions. For example, the “set -e” command allows scripts to exit immediately if any command has a non-zero exit status. In addition, the “set -u” command causes the script to exit if it uses an undefined variable. Moreover, the command “set -x” prints all the commands in the terminal that the script executes. Finally, the “set -o pipefail” command ensures that the pipeline exits with the code of the last executed command with non-zero status or zero if all the commands successfully execute.

Related Articles


<< Go Back to Exit Codes in Bash | Bash Process and Signal Handling | Bash Scripting Tutorial

Rate this post
Md Masrur Ul Alam

Assalamu Alaikum, I’m Md Masrur Ul Alam, currently working as a Linux OS Content Developer Executive at SOFTEKO. I completed my Bachelor's degree in Electronics and Communication Engineering (ECE)from Khulna University of Engineering & Technology (KUET). With an inquisitive mind, I have always had a keen eye for Science and Technolgy based research and education. I strongly hope this entity leverages the success of my effort in developing engaging yet seasoned content on Linux and contributing to the wealth of technical knowledge. Read Full Bio

1 thought on “How to Set & Use Pipefail in Bash [Explained Guide]”

Leave a Comment