Skip to content

Bash Pipefail Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Bash Pipefail Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Bash scripts using set -o pipefail exit unexpectedly when any command in a pipeline fails, even if later commands succeed, because pipefail preserves the exit code of the first failing command.

The Wrong Way

#!/bin/bash
set -eo pipefail

grep "error" logfile.txt | wc -l
echo "Done"

Output:

grep: logfile.txt: No such file or directory

The script exits before printing "Done" because grep fails and pipefail propagates the error.

The Right Way

#!/bin/bash
set -eo pipefail

grep "error" logfile.txt | wc -l || true
echo "Done"

Output:

grep: logfile.txt: No such file or directory
Done

Adding || true prevents a failed command from causing the script to exit.

Step-by-Step Fix

1. Use || true to allow command failure

grep "error" logfile.txt | wc -l || true

2. Capture the actual exit code

set +o pipefail
grep "error" logfile.txt | wc -l
exit_code=${PIPESTATUS[0]}
set -o pipefail
echo "grep exited with: $exit_code"

3. Use a heredoc or temporary file

grep "error" logfile.txt > /tmp/grep_out.txt || true
wc -l < /tmp/grep_out.txt

4. Ignore specific exit codes

grep "error" logfile.txt | wc -l || (( $? == 1 ))  # exit 1 = no match

5. Use the ! prefix to negate exit status

! grep "error" logfile.txt | wc -l

Prevention Tips

  • Always consider which commands in a pipeline might legitimately fail.
  • Use || true for commands where failure is acceptable.
  • Use PIPESTATUS array to get the exit code of each pipeline command.
  • Use set -o pipefail in combination with set -e for strict error handling.
  • Test pipeline commands individually before combining them.

Common Mistakes with pipefail

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world BASH code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What does set -o pipefail do?

Without pipefail, only the exit code of the last command in a pipeline is considered. With set -o pipefail, the pipeline fails if any command fails, using the exit code of the rightmost failed command.

What is the difference between $? and PIPESTATUS?

$? gives the exit code of the last command executed. ${PIPESTATUS[@]} gives an array of exit codes for each command in the last pipeline. ${PIPESTATUS[0]} is the first command's exit code.

How do I use pipefail with set -e safely?

Combine them: set -eo pipefail. But always handle expected failures with || true or check PIPESTATUS explicitly to avoid unexpected exits from well-understood command failures.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro