I/O Redirection

University of Wisconsin Madison

Computer Systems Lab CS1000

Wiscinfo
Wiscinfo
CS Home
CS Home
CSL
CSL
Feedback
CS1000
Feedback
Feedback

next up previous contents index
Next: Pipes Up: Getting Ahead Previous: Getting Ahead

I/O Redirection

   

Many programs in UNIX will by default read their input from the user's keyboard and write their output to the terminal. If you write a C program that uses printf and scanf, the program will act this way. This is convenient for debugging, but what about when it comes time to actually test it with your TA's data? In most systems, you would have to recompile your program. Under UNIX, though, you can dynamically redirect the normal input and output streams right on the command line without ever touching the program's code. For example,

vega1% prog1 < test-data

runs prog1, taking its input from the named file test-data. The output will still be printed on the user's terminal. On the other hand,

vega1% prog1 < test-data > results

still reads from test-data; additionally, it sends the output to the file results. If you had said:

vega1% prog1 < test-data >> results

then the output of the prog1 program would have been appended to the file results, rather than replacing whatever was already there.

Occasionally you may have a program that produces an enormous amount of useless output. You can redirect the output of this program to /dev/null, the little black hole of UNIX. Anything sent here is lost forever. You can even copy files to /dev/null, but this is rather useless.

You may supply the redirections in either order if both are present; the only general rule is that they should follow all regular arguments. This is not technically correct, but you will save yourself some time spent in confusion if you always put the redirection at the end of the command line. For example, the following line mails a file called results to a user named brad at the computer called bay at NYU with the subject test results:

vega1% mail -s "test results" brad@bay.nyu.edu < results  



next up previous contents index
Next: Pipes Up: Getting Ahead Previous: Getting Ahead



Caitlin Howell
Thu Jan 16 20:24:40 CST 1997