Go to the previous, next section.

Exceptions

Unfortunately, not all UNIX commands can have their input or output redirected. As noted before, UNIX commands read from standard input, which is usually the terminal, and write to standard output, which is also usually the terminal. When you specify I/O redirection or piping, the UNIX shell redefines standard input and output for each command to be files or other commands as you specify. The problem is that not all commands use standard input and output.

Some UNIX commands write error output to standard error. Output that goes to standard error can be redirected just like output that goes to standard output. (Note) csh and sh handle standard error redirection slightly differently, csh is explained here)

A notable example is the output of the C compiler that is executed by typing the cc command. If you type the following command, more won't get any input, because the cc command's output will go to standard error:

cc myprog.c | more

To overcome this problem, add an ampersand (`&') after the vertical bar (`|'), as in the following example:

cc myprog.c |& more

Similarly, if you want to send the cc command's output to a file called `error-file', place an ampersand after the right arrow (`>') as in the following example:

cc myprog.c >& error-file

Some commands like vi must talk to the terminal and simply fail under I/O redirection.

Go to the previous, next section.