Go to the previous, next section.

Combining Input / Output redirection and piping

By now, you should begin to see the power of piping and I/O redirection. They provide a highly general way to put together simple commands to perform complex operations.

Programming languages that run under UNIX are usually integrated into this system. Thus if you write an ordinary user program that reads and writes to your terminal, you can tell it to read from a file and write to a file instead. This can save you a lot of time writing and debugging your programs.

For example, suppose you have inserted debugging statements into your code which write out the values of variables at various points in the program. You now find that your program writes so much output that it scrolls off the screen too quickly. You can pipe your program's output through more by typing the following:

myprog | more

Or suppose your program (myprog) prompts you for input at your terminal and you discover a bug that occurs well into the program. When trying to debug the program, you might find that it takes a long time to get to the point where the error occurs. You can tell your program to read its input from a prepared file called input-file instead of your terminal by typing the following:

myprog < input-file

You could combine these steps by using both piping and I/O redirection as follows:

myprog < input-file | more

If you still find you're wading through too much output that's unrelated to the bug, you could search through your program's output for a specific error message that includes the string `uninitialized' as follows:

myprog < input-file | grep uninitialized | more

Go to the previous, next section.