Redirection (computing)
{{Expansion depth limit exceeded| left =
| #default = }}
{{Expansion depth limit exceeded| {{{Expansion depth limit exceeded}}}
| [[File:{{Expansion depth limit exceeded| speedy = Ambox speedy deletion.png
| delete = Ambox deletion.png
| content = Ambox content.png
| style = Edit-clear.svg
| move = Ambox move.png
| protection = Ambox protection.png
| notice
| #default = Ambox notice.png
}} | {{Expansion depth limit exceeded| left = 20x20px
| #default = 40x40px
}} |link=|alt=]]
}}{{Expansion depth limit exceeded| left =
| #default = |
{{{Expansion depth limit exceeded}}} |
[[Category:{{Expansion depth limit exceeded|{{{Expansion depth limit exceeded}}}|{{{Expansion depth limit exceeded}}}}}]][[Category:{{Expansion depth limit exceeded|{{{Expansion depth limit exceeded}}}|{{{Expansion depth limit exceeded}}}}}]]
In computing, redirection is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
In unix-like operating systems programs do redirection with the dup2(2) system call, or its less-flexible but higher-level stdio analogues, freopen(3) and popen(3).
Redirecting standard input and standard output
Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows:
<source lang="bash"> command1 > file1 </source>
executes command1, placing the output in file1. Note that this will truncate any existing data in file1. To append output to the end of the file, use the >> operator.
<source lang="bash"> command1 < file1 </source>
executes command1, using file1 as the source of input (as opposed to the keyboard).
<source lang="bash"> command1 < infile > outfile </source>
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:
<source lang="bash"> command1 | command2 </source>
executes command1, using its output as the input for command2 (commonly called piping, since the "|" character is known as a "pipe").
This is equivalent to using two redirects and a temporary file:
<source lang="bash"> command1 > tempfile command2 < tempfile rm tempfile </source>
A good example for command piping is combining echo with another command to achieve something interactive in a non-interactive shell, e.g.
<source lang="bash"> echo -e "user\npass" | ftp localhost </source>
This runs the ftp client with input user, press return, then pass.
Redirecting to and from the standard file handles
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:
<source lang="bash"> command1 2> file1 </source>
executes command1, directing the standard error stream to file1.
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:
<source lang="bash"> find / -name .profile > results 2>&1 </source>
will try to find all files named .profile. Executed without redirection, it will output hits to stdout and errors (e.g. for lack of privilege to traverse protected directories) to stderr. If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge stderr (handle 2) into stdout (handle 1) using 2>&1 .
It's possible use 2>&1 before ">" but it doesn't work. In fact, 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:
<source lang="bash"> find / -name .profile 2>&1 | less </source>
A simplified form of the command:
<source lang="bash"> command > file 2>&1 </source>
is:
<source lang="bash"> command &>file </source>
or:
<source lang="bash"> command >&file </source>
Chained pipelines
The redirection and piping tokens can be chained together to create complex commands. For example:
<source lang="bash"> ls | grep '\.sh' | sort > shlist </source>
lists the contents of the current directory, where this output is filtered to only contain lines which contain .sh, sort this resultant output lexicographically, and place the final output in shlist. This type of construction is used very commonly in shell scripts and batch files.
Redirect to multiple outputs
The standard command tee can redirect output from a command to several destinations.
<source lang="bash"> ls -lrt | tee xyz </source>
This directs the file list output to both standard output as well as to the file xyz.
References
See also
- Here-document, a way of specifying text for input in command line shells
External links
- Template:Man
- Redirection Definition by The Linux Information Project (LINFO)
- I/O Redirection in The Linux Documentation Project
- Redirection in Windows
- Creating a Child Process with Redirected Input and Output in Windows
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...