Skip to content
Advertisement

Are linux shell pipes pipelined?

Given a file input.txt if I do something like

grep pattern1 input.txt | grep pattern2 | wc -l

is the output from the first command continuously passed (as soon as it is generated) as input to the second command?

Or does the pipe wait until the first command finishes to start running the second command?

Advertisement

Answer

Yes, they’re pipelined — each component’s stdout is connected to the stdin of the next via a FIFO, and all components are started in parallel.


This is why

cat some-file | ...tools... >some-file

…typically results in a truncated file: Because the pipeline is started all at once, the last piece (truncating some-file for write) happens before cat has finished (or often, even started) reading the file from input.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement