Standard Input, Standard Output
and Standard Error

Linux and other Unix-like operating systems feature the concept of standard streams of data. Each command, and therefore each process (i.e., running instance of a program), 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 form (i.e., human readable characters) and are considered to be special types of files.

This terminology can be confusing to new users. However, it is useful to become familiar with it because it is commonly employed in documentation (including the online man pages, which are found on almost all Unix-like systems).

Users familiar with the C programming language (in which the Linux kernel and many of its utilities are written) will be aware that it includes routines to perform basic operations on standard input and output. Examples include printf, which allows text to be sent to standard output, and scanf, which allows a program to read from standard input.

Programs that run from a console (i.e., an all-text display mode) or from a terminal window (i.e., an all-text display window in a GUI) can receive input data in several ways. One is through command line arguments, which are names of files entered on the command line after the name of the program (or command) and any options. In the following example, file1 and file2 are arguments for the wc command:

wc -w file1 file2

That is, file1 and file2 provide the input data for wc, which by default counts the number of lines, words and characters in any given text. The -w option customizes wc by telling it to count only the number of words in each file and ignore the number of lines and characters.

Standard Input

Standard input, often abbreviated stdin, is the input data for a program in the absence of any command line arguments. It is by default any text entered from the keyboard. Thus if wc is typed in at the command line and the ENTER key is pressed without providing any arguments, any text typed in on all subsequent lines will be stored in memory until the wc command is executed by simultaneously pressing the CONTROL and d keys on a new, blank line. wc will then count the lines, words and characters in that stored text.

Standard input can be redirected to come from any text file in place of the keyboard by using the input redirection operator, which is a leftward pointing angular bracket. Thus, to redirect the standard input for the command sort (which sorts lines of text in alphabetic order) from the keyboard to the file named file3, type:

sort < file3

The result is the same as using file3 as an argument, although the mechanism is different, i.e.,

sort file3

A program can alternatively obtain its input from another command through the use of a pipe, which can be considered a type of redirection operator and is represented by the vertical bar character. In the following example, the output of the head command, which by default reads the first ten lines of a file, becomes the standard input for the sort command:

head file3 | sort

Standard Output

Command line programs write to standard output, sometimes abbreviated stdout. Its default destination is generally the display screen, but it can be redirected (e.g., to a file, where it will be saved, or to a printer) or piped to another program to serve as its standard input.

For example, the command file writes the names of files and their types (e.g., text, HTML, GIF or directory) to standard output, which is, as usual, the monitor screen. Thus the following command will display on the screen a list showing the name and type of each file in the current directory (i.e., the directory in which the user is currently working):

file *

In this example, file's argument is the wildcard represented by the star character. A wildcard is a character than can represent some set of characters. The star wildcard can represent any string (i.e., sequence of characters) containing any number of characters, and thus it can represent the name of any item in a directory.

The standard output for this command can easily be redirected to another file using the output redirection operator, which is a rightward facing angular bracket. In the example below, it is redirected to a file named file4:

file * > file4

Or it can be redirected to become the standard input of another program using the pipe operator, such as:

file * | grep directory

Here the filter grep is used to search for any lines in its standard input (which, of course, is the standard output of file *) that contain the sequence of characters directory. Again, because no redirection is used with grep, its standard output is the display screen.

To carry the example a bit further, the standard output of grep could be redirected to a file or to another program. For example, the following pipeline of commands sends grep's standard output to become the standard input for wc -l, which counts the number of lines in text. The number of lines is the same as the number of directories in the current directory, because each directory is listed on a separate line (as is each file). The standard output from wc -l is then redirected to file5, where it is saved for future reference:

file * | grep directory | wc -l > file5

Standard Error

Command line programs send error messages to the user via standard error, abbreviated stderr. As is the case with standard output, its destination is the display screen by default, and it can likewise be redirected (e.g., to a file or printer). Standard error is a separate data stream from standard output in order to allow the two streams to be redirected separately and thus prevent them from becoming intermingled.

As an example of an error message, the cat command, which reads the contents of a file, will produce an error message if an attempt is made to use it to read a non-existent file, such as:

cat nofile

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

Standard error can be redirected with the basic standard error redirection operator, which consists of the numeral 2 followed without an intervening space by a rightward facing angular bracket. In this case, it will create the file to which it is redirected if it does not yet exist, or it will overwrite the contents of the file if a file with the same name already exists. In the following example, the error message will be sent to a file named file6 and will not appear on the display screen:

cat nofile 2> file6

An alternative is to use the standard error appending operator, which appends any error messages to the end of the text in the file rather than overwriting it. This operator, which consists of the numeral 2 followed with no intervening spaces by two rightward facing angular brackets, is useful for error log files.

This page has been deprecated in favor of the following three pages: Standard Input Definition, Standard Output Definition and Standard Error Definition.

Created June 15, 2004. Updated May 20, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.