Chapter 6. Redirecting I/O

Many Unix programs read input (such as a file) and write output in a standard way that lets them work with each other. In this chapter, we discuss some of these tools and learn how to connect programs and files in new and powerful ways.

This chapter generally doesn’t apply to full-screen programs, such as the vi editor, that take control of your whole Terminal window. (less and more do work together in this way, however.) It also doesn’t apply to graphical programs, such as the Finder or Safari, 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 writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol (>) operator. The pipe operator (|) sends the standard output of one program to the standard input of another program. Input/output ...

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