$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Pipes & Pipelines

5 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:13

What a pipe is

The vertical bar | creates an anonymous pipe. The kernel allocates a buffer that has a write end and a read end. The shell gives the write end to the left command as its standard output, and the read end to the right command as its standard input. Neither program sees a file. They read and write bytes, and the kernel carries the bytes between them.

A pipe holds only a limited amount of data at a time. If the reader is slow, the writer blocks when the buffer is full, and if the reader is fast, it blocks waiting for more. This flow control is automatic, which means a pipeline can process far more data than fits in memory.

BASH1 lines
printf 'pear\napple\nfig\n' | sort

The output is apple, fig, pear. The printf builtin wrote the three lines into the pipe, and sort read them all, sorted them, and wrote the result to the terminal.

Building a pipeline

A pipeline is two or more commands joined by pipes. Each command in the chain runs concurrently, not one after another, so the producer and consumer work at the same time. The shell waits for all of them to finish before it shows the next prompt.

BASH1 lines
printf 'b\na\nb\na\nb\n' | sort | uniq -c | sort -rn

The output lists the distinct lines with their counts, highest count first. The first command produces the input, the second groups equal lines, the third counts them, and the last ranks the counts. Each stage is small and does one job, which is what makes the whole chain easy to test and to change.

Exit status of a pipeline

The exit status of a pipeline is the status of its last command. Earlier commands may fail without changing that result. This is a frequent source of silent errors in scripts, because a failing first stage can produce partial output that the last stage accepts happily.

BASH1 lines
ls /no/such | wc -l; echo "status=$?"

The count is 0, and the status is 0 as well, even though ls failed. The status belongs to wc, which succeeded. To see every stage, read the PIPESTATUS array, which holds one status per command in the most recent pipeline.

BASH1 lines
ls /no/such | wc -l; echo "${PIPESTATUS[@]}"

The printed statuses are 2 0, showing that ls failed with 2 and wc succeeded. Inside a script you can also set set -o pipefail, which makes the pipeline status the rightmost non-zero status. That is usually what you want when any failure should count.

Warning

Without pipefail, a failing producer is hidden behind a successful consumer. Enable set -o pipefail in scripts that depend on the data being complete.

Pipes are not files

A pipe has no name, no size, and no stored contents. Once a reader takes bytes out of the buffer, they are gone, and a second reader cannot read them again. This is the main difference from a file, and it is why a pipeline cannot be rewound. If a later stage needs the data twice, the data must be saved somewhere first, which is the job of tee.

A pipe also does not have an end-of-file marker of its own. The reader sees end of file only when every writer has closed its write end. Forgetting that a background process still holds the write end can make a reader wait forever, and this is a common cause of pipelines that hang.

Early exit and SIGPIPE

Sometimes the reader stops before the writer has finished. The classic example is head, which reads a few lines and then exits. When the writer next tries to write to a pipe with no reader, the kernel sends it SIGPIPE, and the writer normally dies quietly. This is why commands such as yes end cleanly when piped into head.

BASH1 lines
yes | head -n 3

The output is three lines of y. The yes command would otherwise write forever, but it stops when head closes the read end. The exit status of this pipeline is the status of head, which is 0.

Each stage runs in a subshell

In bash, every command in a pipeline runs in its own subshell by default. A variable assigned inside a pipeline stage is therefore lost when the pipeline ends. This surprises people who write a loop at the end of a pipe and expect it to update a counter.

BASH3 lines
count=0
printf 'a\nb\n' | while read -r x; do count=$((count+1)); done
echo "count=$count"

The output is count=0, because the loop ran in a subshell and its changes disappeared. A fix is to avoid the pipe, for example by redirecting from a file or using process substitution, which a later lesson covers.

Hands-on: a three-stage pipeline

Build a small report from a list of words. The pipeline below splits the words onto separate lines, sorts them, and counts the distinct entries with the most frequent first.

BASH1 lines
printf 'the cat the dog\n' | tr ' ' '\n' | sort | uniq -c | sort -rn

The first line of output is 2 the. Try replacing sort -rn with sort and observe that the most frequent word is no longer first. The order of stages is part of the design, and each change in a pipeline changes the meaning of the output.

How a pipeline is wired

Visualizer

A pipe is a kernel buffer with a write end and a read end. The shell creates it, forks one child per command, and wires each child to its end before exec.

1 / 6
yeswrites "y" forever
head -1reads one line, exits

Two commands joined by |. Both are started, and the pipe connects their standard streams. Nothing runs one after the other.

yes | head -1; echo "${PIPESTATUS[*]}"
Terminal lab · 3 steps · real Linux in your browser

Build and inspect pipelines

Build a counting pipeline, read PIPESTATUS, and stop a writer with head.

01
Count distinct lines
Sort the three lines of the input, count the distinct ones, and save the result: ``bash printf 'b\na\nb\n' | sort | uniq -c > counts.txt ``
Show hint for step 1
Pipe the output of sort into uniq -c.
02
Read the status of the first stage
The ls fails, but wc succeeds and becomes the pipeline status. Save the status of the first stage: ``bash ls /no/such 2>/dev/null | wc -l echo "${PIPESTATUS[0]}" > status.txt ``
Show hint for step 2
PIPESTATUS is an array, and its first element is the status of the first command in the last pipeline.
03
Stop a writer with head
yes writes forever. Send its output through head so that only three lines are kept: ``bash yes | head -n 3 > three.txt ``
Show hint for step 3
When head exits, yes receives SIGPIPE and stops.
Check yourself Question 1 of 4
Concept Check Pipeline exit status recall

What exit status does a pipeline report by default?

Recap & Key Takeaways

A pipe connects the standard output of the left command to the standard input of the right command, and both run at the same time.
The exit status of a pipeline is the status of its last command, so use PIPESTATUS to inspect every stage and set -o pipefail to catch failures earlier in the chain.
A writer that outlives its reader receives SIGPIPE, which is why yes stops when head exits.
Each stage of a bash pipeline runs in a subshell, so variables assigned inside a pipeline do not persist after it finishes.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue