02 / FILES AND STREAMS · LESSON 08

Pipelines and pipeline status

Detect an upstream pipeline failure instead of trusting only its final command.

13 min · learn at your pace

Connect programs with a pipe

A pipe sends one command’s stdout into the next command’s stdin. Programs run concurrently; the kernel buffers data and applies backpressure when the reader is slower. A pipe is not a string variable, so it can handle streams much larger than the pipe’s buffer.

By default Bash reports the exit status of the last command in a pipeline. If cat missing.txt | tr 'a-z' 'A-Z' cannot read its input, tr may still exit zero after receiving no text. set -o pipefail makes the pipeline return the rightmost nonzero status, or zero if every stage succeeded. This is Bash syntax supported by 3.2, not a portable POSIX sh option.

Handle the result explicitly

Use a pipeline in if ...; then ...; else ...; fi when both outcomes need handling. Capture $? before another command replaces it. Bash also exposes PIPESTATUS, an indexed array of individual stage statuses; reading it must happen immediately after the pipeline.

Some programs use nonzero for a normal condition, such as grep finding no matches. Decide what each command’s statuses mean instead of treating every nonzero value identically or assuming set -e is a complete error-handling strategy.

Try an example.sh
set -o pipefail
cat events.txt | tr 'a-z' 'A-Z'
status=$?
printf 'pipeline=%s\n' "$status"
YOUR TURN

Put it into practice.

Uppercase the input file named by argument 1 (default `events.txt`) into `upper.txt` using a pipeline. On success print `pipeline=0` and exit 0. On an unreadable input print `pipeline=failed` to stderr, print no stdout, and exit nonzero. Suppress the tool’s own diagnostic so this failure message is clear and consistent.

See an explained solution
One way to solve it.sh
set -o pipefail
input=${1:-events.txt}
if cat "$input" 2>/dev/null | tr 'a-z' 'A-Z' > upper.txt; then
    printf 'pipeline=0\n'
else
    printf 'pipeline=failed\n' >&2
    exit 1
fi

pipefail prevents tr from hiding cat’s failure. The explicit branch gives the script a stable diagnostic and nonzero status.