Pipelines


We saw in the previous section that by default commands (generally) print their output in the terminal window. This section introduces the concept of "pipelines", where the output from one command is passed as input to the next command, in order to perform more complex operations.

This concept can be visualized using the following diagram, which shows a pipeline made up of 2 commands:

This pipeline can be executed from the command line as follows:

{command 1} | {command 2}

where the "|" symbol is called a "pipe". This pipeline can be extended to n commands by "piping" the output to additional commands, using:

{command 1} | {command 2} | {command 3} | ... | {command n}

Example Pipeline

As simple example, suppose we have a file containing a list of color names, and we want to generate a sorted list of 5 randomly-selected color names from that list. This can be achieved by combining the following commands:

  1. Execute the shuf command to read colors.txt from the file system, shuffle the lines, then pass the result to stdout, then

  2. Pipe that result to the head command to limit the result to 5 lines:

·
·
·
ninja$: shuf colors.txt | head -n 5
SkyBlue
MediumAquaMarine
Olive
Orchid
LimeGreen
ninja$:  

Next, pipe that result to the sort command to sort the lines:

·
·
·
ninja$: shuf colors.txt | head -n 5 | sort
LimeGreen
MediumAquaMarine
Olive
Orchid
SkyBlue
ninja$:  

which achieves our original goal.