Redirection (computing): Difference between revisions

Content deleted Content added
m Redirecting standard input and standard output: <syntaxhighlight lang="console">
Line 11:
 
===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>command1command > file1</syntaxhighlight> executes {{mono|command1}}, 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>command1command < file1</syntaxhighlight> executes {{mono|command1}}, with {{mono|file1}} as the source of input, as opposed to the [[Computer keyboard|keyboard]], which is the usual source for standard input.
 
<syntaxhighlight lang="bash" inline>command1command < infile > outfile</syntaxhighlight> combines the two capabilities: {{mono|command1}} reads from {{mono|infile}} and writes to {{mono|outfile}}
 
===Variants===
Line 21:
 
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:
<syntaxhighlight lang="bashconsole">
$ tr a-z A-Z << END_TEXT
> one two three
> uno dos tres
> END_TEXT
ONE TWO THREE
UNO DOS TRES
</syntaxhighlight>
 
To read from a string, one can use a [[here string]], using the <code>&lt;&lt;&lt;</code> operator: <syntaxhighlight lang="bash" inline>tr a-z A-Z <<< "one two three"</syntaxhighlight>, or:
 
<syntaxhighlight lang="bashconsole">
$ NUMBERS="one two three"
$ tr a-z A-Z <<< "$NUMBERS"
ONE TWO THREE
</syntaxhighlight>