Passing Command Output as Input to Another Command

Redirection of standard output can also be used to feed the output of one command as the input to a second command. This is called piping.

The Efficiency of Piping

This is an example of piping:

nl acme | lp

Here, the nl command is used to number each line of the acme file. This output is fed to the lp command, which prints the numbered output on the system printer. The acme file is not modified on disk by this process.

You could accomplish the same thing like this:

nl acme > tmpfile
lp tmpfile

In this example, you first number each line and save the result in a temporary file. You then print the temporary file. Piping is more efficient because you don’t have to create a real temporary file. ...

Get Practical UNIX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.