Flowing data through multiple programs in a pipeline
Last revision August 3, 2004
Table of Contents: |
It is also possible to tie the standard output of one program to the standard input of another. This connection is called a pipe. It provides a convenient mechanism for data to flow from one program to another without having to save intermediate results on disk.
The pipe symbol is the vertical line: |
You create a pipe by typing a single command line that contains the names and arguments of the two programs to be connected, with the vertical line symbol between them. Multiple programs can be linked into a single pipeline in one command string.
Examples of pipelines:
- Send the output of a program through the more program in order
to control scrolling of output on the screen. For example, the w
command shows a list of all users logged into the system and some information
about how long they have been logged in and what program they are running. On
pangea, there are usually about 75 to 125 people logged in, so this list cannot
fit on one screen and scrolls rapidly off the top. You can pipe the output from
w to more so that the more command will
control the scrolling on the screen, pausing at the end of each screenful and
waiting for you to press the SPACE bar to continue, or the q
character to quit and discard all remaining output. In general, pipe the output
from all interactive commands that may generate multiple output lines
to more. This allows you to control scrolling and to discard the
remaining output without having to show it. Here is the example command for w:
w | more
- Extending the previous example, you could add another command to the pipeline.
For example, it would be nice to sort the output of w so that multiple
logins by the same account show up in consecutive lines. The sort
program will accomplish this. So the three program pipeline is now:
w | sort | more
Commands and programs that are designed to work in pipes are usually called filters and they read from standard input and write to standard output by default. To use these by themselves, not in a pipe, you must often redirect the input or output to point to a file.
<--Previous | Overview | |