Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Redirection (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Redirecting to and from the standard file handles== In [[Unix shell]]s derived from the original [[Bourne shell]], the first two actions can be further modified by placing a number (the [[file descriptor]]) immediately before the [[token (parser)|character]]; this will affect which stream is used for the redirection.<ref>{{Cite web|url=https://www.redhat.com/sysadmin/redirect-shell-command-script-output|title=How to redirect shell command output|first=Roberto|last=Nozaki|date=April 21, 2022|website=www.redhat.com}}</ref> The Unix standard I/O streams are:<ref>{{Cite web|url=https://www.gnu.org/software/bash/manual/html_node/Redirections.html|title=Redirections (Bash Reference Manual)|website=www.gnu.org}}</ref> {| class="wikitable" ! Handle !! Name !! Description |- | 0 | [[Standard streams#Standard input (stdin)|stdin]] || Standard input |- | 1 | [[Standard streams#Standard output (stdout)|stdout]] || Standard output |- | 2 | [[Standard streams#Standard error (stderr)|stderr]] || Standard error |} For example, {{code|lang=bash|1=command 2> file1}} executes {{mono|command}}, directing the [[Standard streams#Standard error (stderr)|standard error]] stream to {{mono|file1}}. In shells derived from [[C shell|csh]] (the [[C shell]]), the syntax instead appends the {{mono|&}} (ampersand) character to the redirect characters, thus achieving a similar result. The reason for this is to distinguish between a file named '1' and stdout, i.e. {{code|lang=bash|1=cat file 2>1}} vs {{code|lang=bash|1=cat file 2>&1}}. In the first case, stderr is redirected to a file named '{{mono|1}}' and in the second, stderr is redirected to stdout. Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge [[Standard streams#Standard error (stderr)|standard error]] into [[Standard streams#Standard output (stdout)|standard output]] so error messages can be processed together with (or alternately to) the usual output. For example, {{code|lang=bash|1=find / -name .profile > results 2>&1}} will try to find all files named {{mono|.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 {{mono|results}}, error messages appear on the console. To see both hits and error messages in file {{mono|results}}, merge [[stderr]] (handle 2) into [[stdout]] (handle 1) using {{code|2>&1}}. If the merged output is to be piped into another program, the file merge sequence {{code|2>&1}} must precede the pipe symbol, thus, {{code|lang=bash|1=find / -name .profile 2>&1 {{!}} less}} A simplified but non-POSIX conforming form of the command, {{code|lang=bash|1=command > file 2>&1}} is (not available in Bourne Shell prior to version 4, final release, or in the standard shell [[Debian Almquist shell]] used in Debian/Ubuntu): {{code|lang=bash|1=command &>file}} or {{code|lang=bash|1=command >&file}}. It is possible to use <code>2>&1</code> before "<code>></code>" but the result is commonly misunderstood. The rule is that any redirection sets the handle to the output stream independently. So "<code>2>&1</code>" sets handle <code>2</code> to whatever handle <code>1</code> points to, which at that point usually is ''stdout''. Then "<code>></code>" redirects handle <code>1</code> to something else, e.g. a file, but it does '''not''' change handle <code>2</code>, which still points to ''stdout''. In the following example, standard output is written to ''file'', but errors are redirected from stderr to stdout, i.e. sent to the screen: {{code|lang=bash|1=command 2>&1 > file}}. To write both errors and standard output to ''file'', the order should be reversed. Standard output would first be redirected to the file, then stderr would additionally be redirected to the stdout handle that has already been changed to point at the file: {{code|lang=bash|1=command > file 2>&1}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)