# How to Use set -e -o pipefail in Bash, and Why

> While writing scripts in bash, it's important to be careful about error handling. That's what set -e -o pipefail does.

- Author: [Bhagwad Park](https://www.namehero.com/blog/author/bhagwad-park/)
- Published: 2025-09-24
- Updated: 2025-09-24
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/), [VPS Hosting](https://www.namehero.com/blog/category/vps-hosting/)
- Web page: https://www.namehero.com/blog/how-to-use-set-e-o-pipefail-in-bash-and-why/

There are a ton of best practices in Linux scripting that a lot of people ignore. For example, while a lot of people use git, it’s often used unoptimally by those treating it as just a fancy way of saving their work, rather than using it for atomic changes and branches for features. Similarly, many people don’t sanitize their user inputs before using them in scripts, which can cause all kinds of problems when you receive an unexpected input. In bash, set -e -o pipefail is one such example of a best practice that is often ignored. Here’s what it means, and why you should be using it in all your scripts.

## How Error Handling Works in Bash Scripts

By convention in bash, when a command executes successfully, it returns an exit code of zero. A non-zero exit code indicates that something went wrong. It’s up to the person writing the script to catch the error, examine it, and deal with it. But we write so many commands in a single script that it’s impossible to do it all the time, with all the commands.

By default, bash scripts continue executing even when an individual command returns a non-zero error code. This might not cause problems, but it has the potential to cause cascading errors, one after another. For example, consider the following script:

```
#!/bin/bash
mkdir -p /root/myapp/data
echo "DATABASE_URL=postgres://localhost:5432/mydb" > /root/myapp/data/config.txt
CONFIG_VALUE=$(cat /root/myapp/data/config.txt)  # Reads nothing, so empty
echo "Deploying with config: $CONFIG_VALUE"  # Outputs: "Deploying with config: "
curl -X POST http://api.example.com/deploy --data "config=$CONFIG_VALUE"  # Sends empty config!
echo "Script finished – all good!"  # Lies; deployment is now broken elsewhere
```

In this script, we create a directory using mkdir, then create a config file inside that directly, store the contents of the file in a variable, and then use that variable in a “curl” command. This is quite typical of lots of scripts.

But let’s consider what happens if the “mkdir” command fails. If you try to create this directory without the appropriate permissions, here’s what happens:
 [![Mkdir Failed](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-11.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-11.png)Mkdir Failed

And when the very first command in a script fails, the errors cascade down. The directory doesn’t exist, therefore the file doesn’t exist, the variable is empty, and the curl command has sent a meaningless POST request that doesn’t deploy anything.

All because the script failed at the very first command.

And so we need another solution. Something that avoids the problems we get when a script decides to ignore the fact that a command failed and decides to continue executing.

## Using set -e to Immediately Exit on Error

The solution is to include the following code at or near the beginning of the script:

```
set -e
```

Let’s test this. Say I have the following script: So a script would look like this:

```
#!/bin/bash
false  # This command fails (exit code 1)
echo "This will print"
```

In the above script, the “false” command always returns a non-zero exit code, as shown here:
 [![](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124626/image-12.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124626/image-12.png)False Command Error Code

Running this script gives this:
 [![Command Failing and Continuing](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-10.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-10.png)Command Failing and Continuing

As you can see, the command continues to execute even though the “false” statement was executed. The echo command was executed. Now let’s change the script by adding “set -e” to the beginning, like this:

```
#!/bin/bash
set -e
false  # This command fails (exit code 1)
echo "This won't print"
```

Here’s what happens when we run this modified script:
 [![Command Failing and Exiting](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-9.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/22124534/image-9.png)Command Failing and Exiting

This time, with the “set -e” option, the script doesn’t print anything because it exits as soon as it sees the exit code of the “false” command, which it interprets as a failure. So if we have an error that generates a non-zero exit code, the bash will assume that the whole script is corrupted and will exit immediately.

This is a good practice because it prevents silent failure that can cascade down and be extremely difficult to debug later on.

## Setting -o pipefail

Since we’re on the subject of cascading errors, we should address yet another behavior in bash that can suppress error messages accidentally. These are pipe commands of the form:

```
command 1 | command 2
```

We’ve [discussed the pipe operator before](https://www.namehero.com/blog/how-to-use-the-pipe-command-in-linux/), but this nuance goes beyond the regular understanding of the command. By default, piped commands contain the exit status of the last command that executed. So if, for whatever reason, the last command was a success, then the exit status of the entire pipe command will also be a success.

For example, consider the following input:

```
cat nonexistent_file.txt | wc -l
```

In the above command, I want to count the number of lines in a file that I think exists. The problem is that it *doesn’t* exist. And so ideally, the pipe command should fail. But it doesn’t. While the “wc -l” command returns 0 for the number of lines in a non-existent file, it returns a status of “0”, which means success. So the entire command returns a status of “0” as shown here:
 [![Error in Cat is a Successful Pipe Command](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/24114051/image-13.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/24114051/image-13.png)Error in Cat is a Successful Pipe Command

Here, you can see that when we access the last error status via “echo $?”, it shows a status of “0”. If you’re running this in a script and are using a conditional statement to check for errors, you won’t get any warning, even though the “cat” command failed. Based on the error status of this pipe command, your script might continue to execute commands, thinking that everything’s fine.

The solution is to set the following:

```
set -o pipefail
```

The above command will tell bash to propagate the error status of the very first command in the pipe that fails. This means that an exit status of “0” indicates the successful completion of every command in the pipe, and not just the last one. Here’s how it works:
 [![Error in Cat is an Unsuccessful Pipe Command](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/24114117/image-14.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2025/09/24114117/image-14.png)Error in Cat is an Unsuccessful Pipe Command

The commands in the above screenshot are the same as in the previous screenshot, but for one thing – the “set -o pipefail” command right at the top. This now causes the exit status of the command to be “1”, instead of “0” as it was before. The reason is that since “cat nonexistent_file.txt” failed, the error propagates to the end of the pipe and becomes the pipe’s exit status. Now, if you check for errors in this command while scripting, you will be warned in time.

## Combining set -e and set -o pipefail

You can see how these two commands work together. Using “set -e” causes the script to exit if any command fails, and “set -o pipefail” causes a pipe command to fail when any command in the pipe fails. Together, we write them as:

```
set -e -o pipefail
```

This is an example of what we call a “defensive scripting pattern” – a pattern of coding that anticipates things going wrong and guarding against them in advance. It consists of checking for error messages, using exception handling, outputting meaningful error messages, or, as in this case, causing scripts to fail as early as possible before the script can cause any damage. It’s a good pattern to remember, and a little bit of prevention can save you a lot of grief further down the line.

## Conclusion

The ” set -e -o pipefail” command makes scripts robust by combining an early script failure with mandating pipe failures for any command failing inside them. A little prevention can pay significant dividends later and can prevent catastrophic failure from occurring.

## Related posts

- [Bash Debug Mode: set -x and the PS4 Variable](https://www.namehero.com/blog/bash-debug-mode-set-x-and-the-ps4-variable/)
- [Curl Headers, Cookies, and Authentication](https://www.namehero.com/blog/curl-headers-cookies-and-authentication/)
- [Linux Process Management: ps, htop, kill, and nice](https://www.namehero.com/blog/linux-process-management-ps-htop-kill-and-nice/)
