Standard input, standard output, and standard error files

When a command begins running, it usually expects that the following files are already open: standard input, standard output, and standard error (sometimes called error output or diagnostic output).

A number, called a file descriptor, is associated with each of these files, as follows:

Item Description
File descriptor 0 Standard input
File descriptor 1 Standard output
File descriptor 2 Standard error (diagnostic) output

A child process normally inherits these files from its parent. All three files are initially assigned to the workstation (0 to the keyboard, 1 and 2 to the display). The shell permits them to be redirected elsewhere before control is passed to a command.

When you enter a command, if no file name is given, your keyboard is the standard input, sometimes denoted as stdin. When a command finishes, the results are displayed on your screen.

Your screen is the standard output, sometimes denoted as stdout. By default, commands take input from the standard input and send the results to standard output.

Error messages are directed to standard error, sometimes denoted as stderr. By default, this is your screen.

These default actions of input and output can be varied. You can use a file as input and write results of a command to a file. This is called input/output redirection.

The output from a command, which normally goes to the display device, can be redirected to a file instead. This is known as output redirection. This is useful when you have a lot of output that is difficult to read on the screen or when you want to put files together to create a larger file.

Though not used as much as output redirection, the input for a command, which normally comes from the keyboard, can also be redirected from a file. This is known as input redirection. Redirection of input lets you prepare a file in advance and then have the command read the file.