New Document
input / output

I/O redirection and pipelines

Any simple command (or shell function, or compound command) may have its input and output redirected using the following operators. This is performed by the shell before the command is run.
Output redirection
> filename
Standard ouput (file descriptor 1) is redirected to the named file. The file is overwritten unless the noclobber option is set. The file is created if it does not exist.
>> filename
Standard ouput is appended to the named file. The file is created if it does not exist.
>| filename
Output redirect, and override the noclobber option, if set.
Input redirection
< filename
Standard input (file descriptor 0) is redirected to the named file. The file must already exist.
Command pipelines
command | command [ | command ...]
Pipe multiple commands together. The standard output of the first command becomes the standard input of the second command. All commands run simultaneously, and data transfer happens via memory buffers. This is one of the most powerful constructs in Unix. Compound commands may also be used with pipes. Pipes play very nicely with multiprocessor systems.

Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from a place called standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is also your terminal by default.

Output Redirection:

The output from a command normally intended for standard output can be easily diverted to a file instead. This capability is known as output redirection:

If the notation > file is appended to any command that normally writes its output to standard output, the output of that command will be written to file instead of your terminal:

Check following who command which would redirect complete output of the command in users file.

$ who > users

Notice that no output appears at the terminal. This is because the output has been redirected from the default standard output device (the terminal) into the specified file. If you would check users file then it would have complete content:

$ cat users
oko         tty01   Sep 12 07:30
ai          tty15   Sep 12 13:32
ruth        tty21   Sep 12 10:10
pat         tty24   Sep 12 13:07
steve       tty25   Sep 12 13:03
$

If a command has its output redirected to a file and the file already contains some data, that data will be lost. Consider this example:

$ echo line 1 > users
$ cat users
line 1
$

You can use >> operator to append the output in an existing file as follows:

$ echo line 2 >> users
$ cat users
line 1
line 2
$
Redirection Commands:

Following is the complete list of commands which you can use for redirection:

CommandDescription
pgm > fileOutput of pgm is redirected to file
pgm < fileProgram pgm reads its input from file.
pgm >> fileOutput of pgm is appended to file.
n > fileOutput from stream with descriptor n redirected to file.
n >> fileOutput from stream with descriptor n appended to file.
n >& mMerge output from stream n with stream m.
n <& mMerge input from stream n with stream m.
<< tag Standard input comes from here through next tag at start of line.
|Takes output from one program, or process, and sends it to another.

Note that file descriptor 0 is normally standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

Previous                                                                                                                                                       Next

Back to Top