Redirecting I/O and Piping



next up previous contents index
Next: Redirection Up: GETTING FRIENDLY WITH Previous: Modes

Redirecting I/O and Piping

While each Unix command has a standard input and standard output associated with it, the system is actually quite flexible and you can (or can at least try to) make any file or device serve as input or output. This not only frees you from having to type in the input for your program from your terminal and read its output on a screen but also permits you to study a problem at some depth by using and saving various input and output files. Further, you can really get organized and arrange your programs such that different kinds of input data are read from differing devices (aka units or tapes) and then store these files in appropriate places. You may find that as your programs get more powerful, they require more and more complicated input just to control what they do. Yet, you may also find that it is nearly impossible to type in a complicated sequence of control characters without errors (and subsequent disasters). Consequently, editing a master input file and then making only small changes in it is a great time saver and disaster avoider.   

Unix's flexible input and output includes redirection and piping. This system saves you time and file space and can speed up the computation by permitting the computer to look ahead and plan ahead for the work it must do. Most Unix commands get their input from standard input, usually the terminal, and send their output to standard output, also usually the terminal. Redirection and piping allow you to tell a command to read its input from some file and write its output to some other file. Piping allows you to tell a command to read its input from another command's output or even to read its input from the output of a string of several commands without explicitly writing intermediate files. (Unfortunately, not every command gets its input from standard input or sends its output to standard output; in these cases Unix cannot redirect or pipe.)        





next up previous contents index
Next: Redirection Up: GETTING FRIENDLY WITH Previous: Modes