CodeRigor

FREE LESSON

Pipelines, redirection & failure propagation

Connect streams, keep diagnostics separate and detect an upstream failure even when the last command succeeds.

Before this lesson, complete bash-functions.

This course runs GNU Bash with ordinary GNU text tools in a fresh Linux sandbox.

A pipeline connects one command's standard output to the next command's standard input. Standard error stays separate unless redirected. This lets a filter process records while diagnostics remain visible. A stream is not a transaction: a producer may write partial output and fail afterward, so downstream consumers may already have processed that output.

Two process channels
start / 3
Two process channels

Apply the contract

By default Bash reports the status of the last pipeline command. If a producer fails and sort succeeds, the combined status is therefore success. Enable pipefail when a failure anywhere in this pipeline must matter. The resulting status is the rightmost nonzero command status, or zero when all commands succeed. In the lab, normalize any pipeline failure to parent status three while retaining output already produced.

A failure behind a successful sort
start / 3
A failure behind a successful sort
Bash
set -o pipefail
produce() { printf 'partial\n'; return 7; }
if produce | cat; then printf 'success\n'; else printf 'upstream failed\n'; fi
printf 'diagnostic\n' >&2

Reason about the edge cases

Put the pipeline in an if condition and choose the required final status explicitly. Do not assume set -e is a general exception mechanism: its behavior depends on command context and it does not replace a deliberate error policy. Quoting still matters around arguments passed to a function at the start of a pipeline. Variables assigned in pipeline components commonly live in subshells, so do not rely on a while-read pipeline to mutate a parent variable. The executable example demonstrates partial output, an upstream failure and a separate diagnostic; inspect each channel independently.

Normalize the public status
start / 3
Normalize the public status

Practice

Read lines from stdin, keep those starting with ok:, remove that prefix, and print unique remaining lines sorted in the C locale. Ignore other lines. A final input line may lack a newline. In the supplied --fail-source mode the source emits ok:partial and then fails; output the streamed partial line and exit 3. Successful pipelines exit 0. Do not hide an upstream failure behind a successful sort.

Run sample starts again from the first public fixture and grants no mastery evidence. Submit checks all fixtures, including edge cases. Read the specification before editing; the bug and fill phases share the build’s contract. Flashcards reinforce recall on a separate schedule.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Free shell scripting foundation. Use the authored lesson and GNU Bash manual for this lab’s precise argument, status and process contract.