Redirection (computing): Difference between revisions

Content deleted Content added
m Reverted 1 edit by 5.126.233.112 (talk) to last revision by 2601:644:400:C6ED:E5AD:E361:5CC2:C08A. (TW)
 
(48 intermediate revisions by 31 users not shown)
Line 1:
{{noMore footnotescitations needed|date=JanuaryApril 20172024}}
<!-- Note: The following pages were redirects to [[Redirection (computing)]] before draftification:
*[[Redirection operator]]
*[[Redirection (Unix)]]
*[[Redirect (computing)]]
-->
{{Short description|Form of interprocess communication}}
{{distinguish|URL redirection}}
[[Image:Stdstreams-notitle.svg|thumb|300px|The standard streams for input, output, and error]]
 
In [[computing]], '''redirection''' is a form of [[interprocess communication]], and is a function common to most [[command-line interpreter]]s, including the various [[Unix shell]]s that can redirect [[standard streams]] to user-specified locations. The concept of redirection is quite old, dating back to the earliest operating systems (OS).{{cn|date=April 2024}} A discussion of the design goals for redirection can be found already in the 1971 description of the [[input-output]] subsystem of the [[Multics]] OS.{{sfn | Feiertag | Organick | 1972 | p=}} However, prior to the introduction of [[UNIX]] OS with its "[[Pipeline (Unix)|pipes]]", redirection in operating systems was hard or even impossible to do.{{sfn | Kernighan | Morgan | 1982 | p=780 | loc=Input/output redirection}}
 
In [[Unix-like]] operating systems, programs do redirection with the <tt>{{mono|[[dup2]](2)</tt>}} [[system call]], or its less-flexible but higher-level [[Standard streams|stdio]] analogues, <tt>{{mono|[[freopen]](3)</tt>}} and <tt>{{mono|[[popen]](3)}}.</ttref>[https://www.gnu.org/software/libc/manual/html_mono/libc.html The GNU C Library Reference Manual for version 2.38] gnu.org</ref>
 
==Redirecting standard input and standard output==
Line 10 ⟶ 17:
 
===Basic===
Typically, the [[syntax]] of these characters is as follows, using <code>&lt;</code> to redirect input, and <code>&gt;</code> to redirect output. <syntaxhighlight lang="bash" inline>command > file1</syntaxhighlight> executes {{mono|command}}, placing the output in {{mono|file1}}, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will [[Clobbering|clobber]] any existing data in {{mono|file1}}.
 
Using <syntaxhighlight lang="bash" inline>command < file1</syntaxhighlight> executes {{mono|command}}, with {{mono|file1}} as the source of input, as opposed to the [[Computer keyboard|keyboard]], which is the usual source for standard input.
<source lang=bash>
command1 > file1
</source>
 
<syntaxhighlight lang="bash" inline>command < infile > outfile</syntaxhighlight> combines the two capabilities: {{mono|command}} reads from {{mono|infile}} and writes to {{mono|outfile}}
executes <tt>command1</tt>, placing the output in <tt>file1</tt>, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will [[Clobbering|clobber]] any existing data in <tt>file1</tt>.
 
Using
 
<source lang=bash>
command1 < file1
</source>
 
executes <tt>command1</tt>, with <tt>file1</tt> as the source of input, as opposed to the [[Computer keyboard|keyboard]], which is the usual source for standard input.
 
<source lang=bash>
command1 < infile > outfile
</source>
 
combines the two capabilities: <tt>command1</tt> reads from <tt>infile</tt> and writes to <tt>outfile</tt>
 
===Variants===
To append output to the end of the file, rather than clobbering it, the <code>&gt;&gt;</code> operator is used: <syntaxhighlight lang="bash" inline>command1 >> file1</syntaxhighlight>.
 
To read from a stream literal (an inline file, passed to the standard input), one can use a [[here document]], using the <code>&lt;&lt;</code> operator:
<source lang=bash>
<syntaxhighlight lang="console">
command1 >> file1
$ tr a-z A-Z << END_TEXT
</source>
> one two three
> uno dos tres
> END_TEXT
ONE TWO THREE
UNO DOS TRES
</syntaxhighlight>
 
To read from a stream literal (an inline file, passed to the standard input)string, one can use a [[here documentstring]], using the <code>&lt;&lt;&lt;</code> operator: <syntaxhighlight lang="bash" inline>tr a-z A-Z <<< "one two three"</syntaxhighlight>, or:
<source lang=bash>
tr a-z A-Z << END_TEXT
one two three
uno dos tres
END_TEXT
</source>
 
<syntaxhighlight lang="console">
To read from a string, one can use a [[here string]], using the <code>&lt;&lt;&lt;</code> operator:
$ NUMBERS="one two three"
<source lang=bash>
$ tr a-z A-Z <<< "one two three$NUMBERS"
ONE TWO THREE
</source>
</syntaxhighlight>
or:
<source lang=bash>
NUMBERS="one two three"
tr a-z A-Z <<< "$NUMBERS"
</source>
 
==Piping==
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:
[[Image:Pipeline.svg|thumb|A pipeline of three programs run on a text terminal]]
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file. <syntaxhighlight lang="bash" inline>command1 | command2</syntaxhighlight> executes {{mono|command1}}, using its output as the input for {{mono|command2}} (commonly called [[Pipeline (Unix)|piping]], with the "<code>|</code>" character being known as the "pipe").
 
The two programs performing the commands may run in parallel with the only storage space being working buffers (Linux allows up to 64K for each buffer) plus whatever work space each command's processing requires. For example, a "sort" command is unable to produce any output until all input records have been read, as the very last record received just might turn out to be first in sorted order. Dr. Alexia Massalin's experimental operating system, [[Synthesis kernel#Massalin.27s Synthesis kernel|Synthesis]], would adjust the priority of each task as they ran according to the fullness of their input and output buffers.<ref>{{Cite web|url=https://lwn.net/Articles/270081/|title=KHB: Synthesis: An Efficient Implementation of Fundamental Operating Systems Services|website=lwn.net}}</ref>
<source lang="bash">
command1 | command2
</source>
 
executes <tt>command1</tt>, using its output as the input for <tt>command2</tt> (commonly called [[Pipeline (Unix)|piping]], with the "|" character being known as "pipe").
 
The two programs performing the commands may run in parallel with the only storage space being working buffers (Linux allows up to 64K for each buffer) plus whatever work space each command's processing requires. For example, a "sort" command is unable to produce any output until all input records have been read, as the very last record received just might turn out to be first in sorted order. Dr. Alexia Massalin's experimental operating system, [[Synthesis kernel#Massalin.27s Synthesis kernel|Synthesis]], would adjust the priority of each task as they ran according to the fullness of their input and output buffers.
 
This produces the same end result as using two redirects and a temporary file, as in:
 
<sourcesyntaxhighlight lang="bashconsole">
$ command1 > tempfile
$ command2 < tempfile
$ rm tempfile
</syntaxhighlight>
</source>
 
But here, <tt>command2</tt> does not start executing until <tt>command1</tt> has finished, and a sufficiently large scratch file is required to hold the intermediate results as well as whatever work space each task required. As an example, although DOS allows the "pipe" syntax, it employs this second approach. Thus, suppose some long-running program "Worker" produces various messages as it works, and that a second program, TimeStamp copies each record from ''stdin'' to ''stdout'', prefixed by the system's date and time when the record is received. A sequence such as
Worker | TimeStamp > LogFile.txt
Would produce timestamps only when Worker had finished, merely showing how swiftly its output file could be read and written.
 
A good example for command piping is combining <code>[[echo (command)|echo]]</code> with another command to achieve something interactive in a non-interactive shell, e.g.
 
But here, {{mono|command2}} does not start executing until {{mono|command1}} has finished, and a sufficiently large scratch file is required to hold the intermediate results as well as whatever work space each task required. As an example, although DOS allows the "pipe" syntax, it employs this second approach. Thus, suppose some long-running program "Worker" produces various messages as it works, and that a second program, TimeStamp copies each record from ''stdin'' to ''stdout'', prefixed by the system's date and time when the record is received. A sequence such as <syntaxhighlight inline lang="bash">Worker | TimeStamp > LogFile.txt</syntaxhighlight> would produce timestamps only when Worker had finished, merely showing how swiftly its output file could be read and written.
<source lang=bash>
echo -e 'user\npass' | ftp localhost
</source>
 
A good example for command piping is combining <code>[[echo (command)|echo]]</code> with another command to achieve something interactive in a non-interactive shell, e.g. <syntaxhighlight lang="bash" inline>echo -e 'user\npass' | ftp localhost</syntaxhighlight>. This runs the [[File Transfer Protocol|ftp]] client with input <tt>{{mono|user</tt>}}, press <tt>{{mono|return</tt>}}, then <tt>{{mono|pass</tt>}}.
 
In casual use, the initial step of a pipeline is often <code>cat</code> or <code>echo</code>, reading from a file or string. This can often be replaced by input indirection or a [[here string]], and use of cat and piping rather than input redirection is known as [[useless use of cat]]. For example, the following commands:
<sourcesyntaxhighlight lang=bash"console">
$ cat infile | cmdcommand
$ echo $string | cmdcommand
$ echo -e 'user\npass' | ftp localhost
</syntaxhighlight>
</source>
can be replaced by:
<sourcesyntaxhighlight lang=bash"console">
cmd$ command < infile
cmd$ command <<< $string
$ ftp localhost <<< $'user\npass'
</syntaxhighlight>
</source>
As <code>echo</code> is often a shell-internal command, its use is not as criticized as cat, which is an external command.
 
==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"
Line 119 ⟶ 92:
|}
 
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}}.
For example:
 
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.
<source lang="bash">
command1 2> file1
</source>
 
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}}.
executes <tt>command1</tt>, directing the [[Standard streams#Standard error (stderr)|standard error]] stream to <tt>file1</tt>.
 
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}}
In shells derived from [[C shell|csh]] (the [[C shell]]), the syntax instead appends the <tt>&</tt> (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. 'cat file 2>1' vs 'cat file 2>&1'. In the first case, stderr is redirected to a file named '1' and in the second, stderr is redirected to stdout.
 
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}}.
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. Example:
 
It is possible to use <code>2>&1</code> before "<code>></code>" but the result is commonly misunderstood.
<source lang="bash">
find / -name .profile > results 2>&1
</source>
 
will try to find all files named <tt>.profile</tt>. 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 <tt>results</tt>, error messages appear on the console. To see both hits and error messages in file <tt>results</tt>, merge [[stderr]] (handle 2) into [[stdout]] (handle 1) using '''<tt>2>&1</tt>''' .
 
<!-- *** This is nonsense. Redirection works perfectly fine, just not as you may have expected. ***
It's possible use <tt>2>&1</tt> before "<tt>></tt>" but it doesn't work. In fact, when the interpreter reads <tt>2>&1</tt>, 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 '''<tt>2>&1</tt>''' must precede the pipe symbol, thus:
 
<source lang="bash">
find / -name .profile 2>&1 | less
</source>
 
A simplified but non-POSIX conforming form of the command:
 
<source lang="bash">
command > file 2>&1
</source>
 
is (not available in Bourne Shell prior to version 4, final release, or in the standard shell [[Debian Almquist shell]] used in Debian/Ubuntu):
 
<source lang="bash">
command &>file
</source>
 
or:
 
<source lang="bash">
command >&file
</source>
 
<u>'''NOTE:'''</u> 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.
 
<source lang="bash">
command 2>&1 > file
</source>
 
ToIn writethe bothfollowing errors andexample, standard output is written to ''file'', thebut ordererrors shouldare beredirected reversed.from Standard output would first be redirectedstderr to the filestdout, theni.e. stderr would additionally be redirectedsent to the stdoutscreen: handle{{code|lang=bash|1=command that2>&1 has already been changed to point at the> 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}}.
<source lang="bash">
command > file 2>&1
</source>
 
==Chained pipelines==
The redirection and piping tokens can be chained together to create complex commands. For example, <syntaxhighlight lang="bash" inline>sort infile | uniq -c | sort -n > outfile</syntaxhighlight> sorts the lines of {{mono|infile}} in lexicographical order, writes unique lines prefixed by the number of occurrences, sorts the resultant output numerically, and places the final output in {{mono|outfile}}.<ref>{{Cite web|url=https://www.linux.org/threads/piping-and-redirecting-output-in-the-linux-terminal.43745/|title=Piping and Redirecting Output in the Linux Terminal |website=Linux.org}}</ref> This type of construction is used very commonly in [[shell script]]s and [[batch file]]s.
The redirection and piping tokens can be chained together to create complex commands. For example:
 
<source lang="bash">
sort infile | uniq -c | sort -n > outfile
</source>
 
sorts the lines of <tt>infile</tt> in lexicographical order, writes unique lines prefixed by the number of occurrences, sorts the resultant output numerically, and places the final output in <tt>outfile</tt>. This type of construction is used very commonly in [[shell script]]s and [[batch file]]s.
 
==Redirect to multiple outputs==
The standard command <tt>{{mono|[[tee (command)|tee]]</tt>}} can redirect output from a command to several destinations:<syntaxhighlight lang="bash" inline>ls -lrt | tee xyz</syntaxhighlight>. This directs the file list output to both standard output and the file {{mono|xyz}}.
 
==See also==
<source lang="bash">
* [[Here-document]], a way of specifying text for input in command-line shells
ls -lrt | tee xyz
* [[Shell shoveling]]
</source>
* [[Command substitution]]
* [[Process substitution]]
* [[Console redirection]]
 
== References ==
This directs the file list output to both standard output and the file <tt>xyz</tt>.
{{Reflist}}
 
==See alsoSources ==
* {{cite journal | last=Feiertag | first=R. J. | last2=Organick | first2=E. I. | title=The Multics input/output system | journal=ACM SIGOPS Operating Systems Review | volume=6 | issue=1/2 | date=1972 | issn=0163-5980 | doi=10.1145/850614.850622 | pages=35–38}}
*[[Here-document]], a way of specifying text for input in command line shells
* {{cite journal | last=Kernighan | first=Brian W. | last2=Morgan | first2=Samuel P. | title=The UNIX Operating System: A Model for Software Design | journal=Science | publisher=American Association for the Advancement of Science | volume=215 | issue=4534 | year=1982 | eissn=00368075 | issn=10959203 | jstor=1687467 | pages=779–783 | url=http://www.jstor.org/stable/1687467 | access-date=2024-04-25}}
*[[Shell shoveling]]
*[[Command substitution]]
*[[Process substitution]]
 
==External links==
Line 219 ⟶ 144:
[[Category:Unix]]
[[Category:Windows administration]]
 
[[pl:Standardowe strumienie]]