LINFO

Standard Output Definition


Standard output, sometimes abbreviated stdout, refers to the standardized streams of data that are produced by command line programs (i.e., all-text mode programs) in Linux and other Unix-like operating systems.

These standardized streams, which consist of plain text, make it very easy for the output from such programs to be sent to a devices (e.g., display monitors or printers) or to be further processed by other programs. The introduction of standardized streams represented a major breakthrough in the computer field when it was incorporated into the original UNIX operating system more than three and a half decades ago, because it eliminated the very complex and tedious requirement of having to adjust the output of each program according to the specific device or program to which it was being sent.

One of the features of standard output is that it has a default destination but can easily be redirected (i.e., diverted) to another destination. That default destination is the display screen on the computer that initiated the program. Because the standard streams are plain text, they are by definition human readable.

Thus, for example, the following command will cause the head program, whose default behavior is to read the first ten lines of any specified files, to send the first ten lines of the file named file1 to the display screen:

head file1

Redirection operators can be used to redirect the output of a command line program from the display screen to a file where it is written and saved, to a printer where it is printed, or to another program where it is used as an input. In the following example the output redirection operator, which is represented by a rightward pointing angular bracket, is used to redirect head's output from the display screen to a file named file2:

head file1 > file2

That is, the output from file1 is written to file2 instead of being displayed on the screen. The output redirection operator will cause the contents of file2 to be overwritten if a file with the same name already exists, and it will create a file with that name if it does not yet exist.

As another example, the file command is used to report the types of files, such as whether they are text, HTML, GIF, WAV or compressed files or whether they are links or directories (which are both treated as types of files in Unix-like operating systems). It sends to standard output a line containing the file name and type for each file whose name is provided to it as an argument (i.e., an input). Thus, the following will display on the monitor screen a list containing the name and type of every file in the current directory (i.e., the directory in which the user is currently working):

file *

The asterisk is a wildcard that means all or everything. In this case, it tells the file command to consider every file in the current directory as an argument.

The output from the file command (as is the case with head or any other command) can easily be redirected to another file using the output redirection operator. For example, the following causes the list of the names and types of the files (and thus the directories) in the current directory to be written to a file named file3 instead of being displayed on the screen:

file * > file3

Output from one command line program can be redirected to become the input of another command line program by using the pipe operator, which is represented by a vertical bar character. Thus, for example, the list of file names and types from the above examples could be redirected to the grep command, whose function is to search text for specified strings (i.e., sequences of characters). In the following example, grep is used to find any lines in that list which contain the string directory and to send those lines to standard output (which will be the display screen because grep's output is not redirected):

file * | grep directory

To carry the example further, the output of grep could easily be redirected from the display screen to a file, to a printer or to another program (just as was done with the head and file commands in the preceding examples). For example, it could be redirected to the wc program, whose default behavior is to count the number of lines, words and characters in a file or other input.

The following pipeline of commands (i.e., two or more commands connected by a pipe or pipes) redirects grep's standard output to become the input for wc -l.

file * | grep directory | wc -l

The -l option tells wc to only count the number of lines and to ignore the counts of words and characters. 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 of every other type).

The output from wc -l can likewise be redirected from the display monitor, for example to a file named file4:

file * | grep directory | wc -l > file4

Thus, the above pipeline of commands counts the number of directories in the current directory and writes that number to file4 (which is created if it does not already exist, or is overwritten if it already exists). This can be easily confirmed by using the head command or the cat command (one of the most popular commands for reading files) to read the file, i.e.,

cat file4

Here the cat command writes an integer representing the total number of directories to standard output, which is, of course, the monitor screen.

Not all programs have a standard output, and thus nothing normally appears on the display screen when they are used. Examples include cp, which copies files and directories, mkdir, which creates directories and mv, which renames and moves files and directories.

The input counterpart of standard output is standard input. Often abbreviated stdin, it is the source of input(s) for a program. Just as standard output is by default the display screen, standard input is by default any text entered from the keyboard. However, the input to most command line programs can likewise be redirected, and it can come from files whose names are names provided as arguments or from the output of another program.

Standard output and standard input are important parts of the traditional terminology for Unix-like operating systems. Although such terminology can be confusing for new users, experienced users find it very useful both because of the precision that it adds to explanations and because it is widely used (including in the online man and info documentation typically installed on Unix-like operating systems).




Created February 10, 2005. Updated April 2, 2007.
Copyright © 2005 The Linux Information Project. All Rights Reserved.