Chapter 6. Redirecting I/O

Many Unix programs read input (such as a file) and write output. In this chapter, we discuss Unix programs that handle their input and output in a standard way. This lets them work with each other.

This chapter generally doesn’t apply to full-screen programs, such as the vi editor, that take control of your whole Terminal window. (The pager programs, less, and more do work together in this way.) It also doesn’t apply to graphical programs, such as the Finder or Internet Explorer, that open their own windows on your screen.

Standard Input and Standard Output

What happens if you don’t give a filename argument on a command line? Most programs will take their input from your keyboard instead (after you press Return to start the program running, that is). Your Terminal keyboard is the program’s standard input .

As a program runs, the results are usually displayed on your Terminal screen. The Terminal screen is the program’s standard output . So, by default, each of these programs takes its information from the standard input and sends the results to the standard output. These two default cases of input/output (I/O) can be varied. This is called I/O redirection .

If a program doesn’t normally read from files, but reads from its standard input, you can give a filename by using the < (less-than symbol) operator. tr (character translator) is such a program. Here’s how to use the input redirection operator to convert commas to linefeeds in the to_do file:

% cat ...

Get Learning Unix for Mac OS X, Second 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.