Posts Tagged ‘stderr’

Pipes, Redirection and Chaining

Wednesday, March 12th, 2008

Please read about Standard-output and Standard-input first.

Even though most people consider the command-line a thing of the past, there are some really interesting things you can do with the terminal that enables it to be more powerful than the gui interface.

Two advantages are the use of pipes, chaining, and redirection. With pipes, chaining, and redirection, you can “chain” multiple programs and functions to become extremely powerful commands. Most programs on the command-line accept different modes of operation. Many can read and write to files for data, and most can accept standard input or output. This means that you can direct the output of one program as input to another program. You can then take the output of the second program and redirect it as input to yet another program, or redirect the output to a file.

In fact, most programs have two modes of output: standard output (stdout for short) and standard error (stderr). These are both different streams of output that are distinct from each other, which can allow you to redirect the stderr of a program to the same “stream” as the standard input (stdin), or redirect them to different places.

(more…)

Stdin, stdout and stderr

Tuesday, March 11th, 2008

There are three types of standard streams for Unix-like operating systems. Standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. The three I/O connections are called standard input, standard output and standard error.

From Wikipedia:

Standard input (stdin)

Standard input is data (often text) going into a program. The program requests data transfers by use of the read operation. Not all programs require input. For example, the dir or ls program (which displays file names contained in a directory) performs its operation without any stream data input. Unless redirected, input is expected from the text terminal which started the program.

Standard output (stdout)

Standard output is the stream where a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example the file rename command (variously called mv, move, ren) is silent on success.

Standard error (stderr)

Standard error is another output stream typically used by programs to output error messages or diagnostics. It is a stream independent of standard output and can be redirected separately. The usual destination is the text terminal which started the program to provide the best chance of being seen even if standard output is redirected (so not readily observed). For example, output of a program in a pipeline is redirected to input of the next program, but errors from each program still go directly to the text terminal.

It is acceptable—and normal—for standard output and standard error to be directed to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless buffering is involved. (For example, a common situation is when the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream’s buffer is not yet full.)

(more…)