Shell Streams and Pipes
Learn how Unix-like shells route standard input, output, and error, and how pipes connect command stages.
Streams Through a Pipeline
Three streams, three roles
A command can read from (file descriptor 0), send normal results to (1), and send diagnostics or errors to (2). In a typical Unix-like shell, both output streams appear in the terminal by default. They may look mixed together, but they remain separate streams.
Follow the data through a
A regular (|) connects the first command’s to the next command’s . This lets commands process data in stages without saving intermediate results to a file.
Consider:
printf '%s\n' apple pear apple | sort | uniq -c
First, printf prints three lines. The first sends those lines to sort, which orders them. The second sends the sorted lines to uniq -c. Because duplicate lines are now adjacent, uniq -c can count them: the result represents two apple lines and one pear line.
The regular pipes carry only. If an earlier command writes an error message to , that message usually remains visible in the terminal instead of going to the next command. In Bash, |& also sends the earlier command’s through the by merging it with first.