LINFO

Standard Error Definition



Standard error, abbreviated stderr, is the destination of error messages from command line (i.e., all-text mode) programs in Unix-like operating systems.

Unix-like operating systems feature the concept of standard streams of data. Each process is automatically initialized with (i.e., assigned) three data streams: one input stream, called standard input, and two output streams, called standard output and standard error. These streams consist of data in plain text (i.e., human readable characters) and are considered to be special types of files. A process is an instance of an executing program.

Standard output, sometimes abbreviated stdout, refers to the destination of the output from command line programs exclusive of error messages, and it is the display screen by default. Standard input is the source of input data for a command line program, and by default it is any text entered from the keyboard. A command is an instruction telling a computer to do something, such as run a program.

As is the case with standard output, standard error is also the display screen by default, and it can likewise be redirected (i.e., sent to a destination other than its default destination) to a file, printer, another program, etc. The reason that standard error is a separate data stream from standard output is so that the two streams to be redirected separately and thereby prevent them from becoming intermingled.

As an example of an error message, the cat command, among whose functions is to read the contents of files, will produce an error message if an attempt is made to use it to read a non-existent file, such as a file named nofile, i.e.,

cat nofile

In this case, an error message similar to the following will appear on a new line on the monitor screen (because the standard error was not redirected): cat: nofile: No such file or directory .

Standard error can be redirected with the standard error redirection operator, which is designated by the numeral 2 followed directly (i.e., with no intervening space) by a rightward pointing angular bracket (i.e., 2>). This operator will create the file to which standard error is redirected if a file with that name does not yet exist, or it will overwrite the contents of the file if a file with the same name already exists. For instance, the standard error from the above example could be redirected from appearing on the display screen to being written to a file named file1 as follows:

cat nofile 2> file1

An alternative is to use the standard error appending operator, designated by the numeral 2 followed by two rightward pointing angular brackets (i.e., 2>>), which appends any error messages to the end of the text in the designated file rather than overwriting it. This operator is useful for maintaining error log files.

As an example of the separate redirection of the two output streams from a single command, the wc command (whose default behavior is to count the lines, words and characters in text) can be used to attempt to provide counts for two files, nofile, which does not exist, and file1, which does exist. Standard output is redirected to the file wordcount using the output redirection operator (designated by a single rightward facing angular bracket), and standard error is redirected to the file errorlog with the standard error appending operator:

wc nofile file1 > wordcount 2>> errorlog

If the files wordcount and errorlog do not already exist, they will be created. As usual, the contents of these files can be confirmed by using cat or a text editor, i.e.,

cat wordcount

and

cat errorlog

If it is desired to redirect both standard output and the standard error to the same destination, the combined output and error redirection operator, which is designated by a rightward facing angular bracket followed immediately by an ampersand (i.e., >&) can be used, e.g.,

wc nofile file1 >& wordcount

On the bash shell, the default shell on Linux, an ampersand followed by a rightward facing anglular bracket (i.e., &>) can also be used for this purpose.

Error messages can be discarded so that they neither appear on the screen nor are written to any file by redirecting them to a special file called /dev/null, into which everything sent will disappear without a trace. For example:

cat nofile1 2> /dev/null






Created May 12, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.