Course contentsShow
← Linux
Lesson 139 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Exit Code Propagation

Design pipelines that preserve failure states across multiple command stages.

In this lesson, you will learn how to ensure that if a single command in a multi-stage pipeline fails, the entire pipeline reports that failure correctly to your script.

The Pipeline Problem

When you run commands in a pipeline—such as command1 | command2 | command3—Bash usually only cares about the exit code of the last command in the chain. If command1 fails but command3 succeeds, Bash considers the whole pipeline a success. This is dangerous because errors in data processing or fetching can be silently ignored, leading to corrupted output or downstream bugs.

Enabling Pipefail

To fix this, Bash provides the set -o pipefail option. When this is enabled, the pipeline’s exit code is determined by the last command to exit with a non-zero status. If all commands succeed, the pipeline returns 0. If any command fails, the pipeline returns the exit code of the rightmost command that failed.

Consider this example:

#!/bin/bash

# Enable exit code propagation for pipelines
set -o pipefail

# If 'curl' fails (e.g., 404 error), the pipe will now return that error code
# even though 'grep' might have succeeded.
curl -s http://example.com/data | grep "important_pattern" > output.txt

if [ $? -ne 0 ]; then
    echo "The pipeline failed. Check your network or the source file."
    exit 1
fi

Why This Matters

By using set -o pipefail, you turn your pipelines into robust units of logic. You no longer have to worry about a failure being swallowed by subsequent commands in the stream. It forces your script to acknowledge the reality of the entire chain of execution rather than just the final participant.

When combining this with your knowledge of Conditional Execution Strategies (using && or ||), you can create scripts that stop immediately when a mid-pipeline error occurs, preventing partial data from causing issues later in your workflow.

Key Takeaway: Use set -o pipefail to ensure that a failure anywhere in a command pipeline is correctly reported as the exit status of the entire sequence.