Redirection (Linux)

From Christoph's Personal Wiki
Revision as of 19:11, 20 October 2009 by Christoph (Talk | contribs)

Jump to: navigation, search

Redirection is a function common to most Linux shells which allow standard streams to be redirected to user-specified locations.

Redirecting standard input and standard output

Redirection is usually implemented by placing certain characters (or tokens) between commands. Typically, the syntax of these characters is as follows:

command1 > file1  

executes command1, placing the output in file1.

command1 < file1 

executes command1, using file1 as the source of input (as opposed to the keyboard).

command1 < infile > outfile

combines the two capabilities: command1 reads from infile and writes to outfile

Piping

Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:

command1 | command2   

executes command1, using its output as the input for command2 (commonly called piping, since the "|" character is known as a "pipe").

A good example for command piping is combining echo with another command to achieve something interactive in a non-interactive shell, e.g.

echo -e "user\npass" | ftp localhost

This would run the ftp client and enter user, press return, then pass.

Redirecting to and from the standard file handles

In shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Linux standard I/O streams are:

handle name description
0 (stdin) standard input
1 (stdout) standard output
2 (stderr) standard error

For example:

command1 2> file1

executes command1, directing the standard error stream to file1 (useful since standard error outputs to the terminal by default and is unaffected by redirection unless so specified).

In shells derived from csh (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result.

Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example:

find / -name .profile > results 2>&1

will try to find files/directories named CVS in the /root directory. Executed without redirection, it will output hits to stdout and errors (e.g. for lack of privilege to read certain directories) to Stderr. If we redirect standard output to file results, the error messages will continue to spam the console. To see both hits and error messages in file results, we merge stderr (handle 1) into stdout using 2>&1 .

If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus:

find / -name .profile 2>&1 | less

Chained pipelines

The redirection and piping tokens can be chained together to create complex commands, for example:

ls | grep '.sh' | sort > shlist    

lists the contents of the current directory, where this output is filtered to only contain lines which contain .sh, sort this resultant output alphabetically, and place the final output in shlist.

This type of construction is used very commonly in Linux shell scripts.

Common standard I/O redirections

Note: These have been tested on a bash shell.

Common Standard I/O Redirections
Function csh sh
Send stdout to file prg > file prg > file
Send stderr to file prg 2> file
Send stdout and stderr to file prg >& file prg > file 2>&1
Take stdin from file prg < file prg < file
Send stdout to end of file prg >> file prg >> file
Send stderr to end of file prg 2>> file
Send stdout and stderr to end of file prg >>& file prg >> file 2>&1
Read stdin from keyboard until c prg <<c prg <<c
Pipe stdout to prg2 prg | prg2 prg | prg2
Pipe stdout and stderr to prg2 prg |& prg2 prg 2>&1 | prg2


External links