UNIX uses three standard streams for input and output: stdin, stdout, and stderr. stdin is the place from which input comes to a process, stdout is the place where normal output goes, and stderr is the place where error or diagnostic output goes. So a standard unix process looks something like a pipe fitting with one input and two outputs.
By default, the input is attached to the input of the terminal starting the process (i.e., your keyboard), and both the output streams are attached to the output of the terminal (i.e., your screen).
Redirection lets you change these attachment points.
There are two basic flavors of redirection, to and from files which uses "<" and ">" characters, and to and from other processes which uses the "|" (pipe) character.
% ls > file
So stdin and stderr are still attached to the terminal, but anything sent to stdout goes to the file.
If the file already exists, it is truncated before the process is spawned. (unless the "noclobber" shell variable is set in which case redirecting to an existing file is an error.)
If a double angle bracket is used (">>") then the output is appended to the file.
% ls >& file
Here stdout and stderr get combined onto stdout which then gets shunted into the file.
Again, if a double angle bracket is used (">>&") then the output is appended to the file.
% wc < file
Here stdout and stderr remain attached to the terminal, but input is read from the file.
% wc << END This is some text that will be counted by wc(1). END
% ls | wc
So the first process's stdout feeds into the second's stdin, both process's stderr still goes to the terminal, and the second process's stdout goes to the terminal.
% ls |& wc
So the first process's stderr is combined with stdout which feeds into the second's stdin, the second process's stderr and stdout go to the terminal.
% sed 's/^#//' < file | wc -l | awk '{print $NF}' >& otherfile
Here stdin for the first process comes from a file, stdout for the first process goes to stdin of the second, stdout of the second goes to stdin of the third, and stdout and stderr of the third goes into another file. The first and second process's stderr goes to the terminal.
Up to the TTTT index
Tuesday Tiny Techie Tips are all © Copyright 1996-1997 by Jeff Youngstrom. Please ask permission before reproducing any of this material.