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.
printf 'pear\napple\nfig\n' | sortThe 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.
printf 'b\na\nb\na\nb\n' | sort | uniq -c | sort -rnThe 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.
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.
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.
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.
yes | head -n 3The 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.
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.
printf 'the cat the dog\n' | tr ' ' '\n' | sort | uniq -c | sort -rnThe 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.