Is there a way to catch a failure in piped commands?

bash, error-handling, pipe, shell

Solution

Since you are using bash, other than `set -e` you can also add `set -o pipefail` to get the results you want...

Problem

Here's an example of what I'm trying to achieve: ``` #!/bin/bash set -e # abort if error ... command1 2>&1 | command2 ... ``` And I notice that sometimes `command1` fails but command2 does not and the shell script happily continues... if I did not have to use the pipe here, the `set -e` would have been sufficient but now it does not work with the pipe there... Any thoughts? Thanks

Original source

Related problems