DEV Community

Schiff Heimlich
Schiff Heimlich

Posted on

The pipefail Gotcha That Bit Me in a Deploy Script

I had a deploy script that worked fine for months. Then I added set -o pipefail and things started breaking in ways that took me way too long to debug.

The issue was a pipeline like this:

cat deploy.log | grep "ERROR" | wc -l
Enter fullscreen mode Exit fullscreen mode

Simple enough — count error lines. If count is above zero, abort. But when the log was clean, grep returned exit code 1 (no match found). Without pipefail, the pipeline's exit code came from wc -l (which returned 0), so the script kept going.

That seemed fine until I turned on pipefail to catch real failures. Suddenly a clean log (no errors) was killing my script because grep's exit code 1 propagated through the pipeline, and set -e did the rest.

The Fix

grep -c gives you the count directly, and || true keeps set -e from aborting:

ERRORS=$(grep -c "ERROR" deploy.log || true)
Enter fullscreen mode Exit fullscreen mode

This returns 0 on a clean log without triggering an error exit.

Another One: head Causing SIGPIPE

set -o pipefail
cat data.csv | awk -F, '{print $2}' | sort | head
Enter fullscreen mode Exit fullscreen mode

When head grabs its 10 lines and closes the pipe, sort gets SIGPIPE and exits non-zero. With pipefail, your script dies even though it did exactly what you wanted.

What I Do Now

Keep pipefail on for critical paths. Disable it locally when a non-zero exit is expected behavior:

set +o pipefail
COUNT=$(grep -c "pattern" file || true)
set -o pipefail
Enter fullscreen mode Exit fullscreen mode

It took me embarrassingly long to realize that pipefail doesn't just catch real errors — it also surfaces the "normal" non-zero exits that we usually ignore. If you're adding pipefail to an existing script, grep through your pipelines first. Anything using grep in a pipe is probably going to bite you.

Top comments (0)