Input output redirection


Standard Input, Standard Output and Standard Error

In general, a command (a program):

  • Gets data to process fromstandard input or stdin(default: keyboard).
  • Returns processed data tostandard output or stdout(default: screen).
  • If program execution causes errors, error messages are sent to standard error orstderr (default: screen).

These three are files, and are always open. As all open files, they are assigned to a file descriptor (an integer).

File descriptors for stdinstdout and stderr.
FileFile descriptor
/dev/stdin or /dev/fd/00
/dev/stdout or /dev/fd/11
/dev/stderr or /dev/fd/22


Capturing output from a file or a command (or program or script) and sending it as input to another file or command (or program or script).
(pipe) redirects standard output of a program as standard input of another.
redirects standard output to a file (and overwrites the file if it already exists).
uses a file as standard input.
Appends standard output to a file.
< Input_data_file  program1 | program2 > Output_data_file

A "filter" is a program which reads data from standard input, processes it in some way, and sends the processed data to standard output.

49Redirections Examples

2> file
Direct standard error to file
2>> file
Append standard error to file.
Print standard error messages to standard output.
Print standard output messages to standard error.

50Building Command Lines From Standard Input

Build and execute command lines from standard input.

Read remaining arguments from standard input instead of specifying them and execute the command.

cd /bin
ls | xargs whatis