OpenWindows Advanced User's Guide

2.2.5 Redirecting and Piping Command Output

Unless you indicate otherwise, commands normally display their results on the screen. There are special symbols that allow you to redirect the output of a command. For example, you may want to save the output to a file rather than display it on the screen. The following example illustrates the use of the redirect symbol (>):

$ date > sample.file

In this example, the output from the date command is redirected to a new file called sample.file. Next, the contents of sample.file are displayed with the more command:

$ more sample.file
Mon Feb 3 12:56:26 PST 1993

As you can see, the contents of sample.file now contains the output from the date command. (See Chapter 3, Working with Files and Directoriesfor information on the more command.)

Sometimes you may want to redirect the output of one command as input to another command. A set of commands strung together in this fashion is called a pipeline. The symbol for this type of redirection is a vertical bar (|) called a pipe.

For example, instead of saving the output of a command to a file, you may want to direct it as input to the command for printing (lp) by using the pipe symbol (|). To send the output from the date command directly to the printer, you would enter the following:

$ date | lp

This would print the results of the date command. (See "8.1.1 Submitting Print Requests to the Default Printer"for information on using the lp command to print files.)

The command redirection examples shown here are quite simple, but when you learn more advanced commands, you will find that there are many uses for piping and redirection.