Redirection (Unix)

From Wikipedia, the free encyclopedia

The standard streams for input, output, and error
The standard streams for input, output, and error

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

Programmatically, it is done with the dup2(2) system call, or its less-flexible but higher-level stdio analogues, freopen(3) and popen(3).

Contents

Redirection is usually implemented by placing certain characters between commands. In this context, the characters are often referred to as hoinkies in order to "avoid confusion with other bracket-type operators" (Bryant and O'Hallaron 2003). 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

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

A pipeline of three programs run on a text terminal
A pipeline of three programs run on a text terminal
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 enter pass.

In Unix 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 Unix 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 .profile 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 2) into stdout (handle 1) using 2>&1 .

It's possible use 2>&1 before ">" but it doesn't work. Infact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged.

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

A simplified form of the command:

command > file 2>&1

is:

command &>file

or:

command >&file

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 Unix shell scripts.

  • Bryant, Randal E.; David O'Hallaron (2003). Computer Systems: A Programmer's Perspective. Pearson Education, Inc., 794. ISBN 0-13-034074-X.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.