Chapter 5. Redirecting I/O

Standard Input and Standard Output

Many UNIX commands read input (such as a file) and write output.

In general, if no filename is specified in a command, the shell takes whatever you type on your keyboard as input to the command (after you press the first RETURN to start the command running, that is). Your terminal keyboard is the command’s standard input.

As a command runs, the results are usually displayed on your terminal screen. The terminal screen is the command’s standard output.

So, by default, each command takes its input from the standard input and sends the results to the standard output.

These two default cases of input/output can be varied. This is called input/output redirection. You can use a given file as input to a command that doesn’t normally accept filenames by using the “<” (less-than symbol) operator. For example, the following command mails the contents of the file to_do to bigboss@corp:

% mail bigboss@corp < to_do
%

You can also write the results of a command to a named file or some other device instead of displaying output on the screen using the > (greater-than symbol) operator. The pipe operator | sends the standard output of one command to the standard input of another command. Input/output redirection is one of the nicest features of UNIX because of its tremendous power and flexibility.

Putting Text in a File

Instead of always letting the output of a command come to the screen, you can redirect output into a file. ...

Get Learning the UNIX Operating System, Fourth Edition 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.