In computing, redirection is a form of interprocess communication, and 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).
Redirection is usually implemented by placing certain characters between commands.
Typically, the syntax of these characters is as follows, using <
to redirect input, and >
to redirect output.
executes command1, placing the output in file1, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will clobber any existing data in file1.
Using
executes command1, with file1 as the source of input, as opposed to the keyboard, which is the usual source for standard input.
combines the two capabilities: command1 reads from infile and writes to outfile
To append output to the end of the file, rather than clobbering it, the >>
operator is used:
To read from a stream literal (an inline file, passed to the standard input), one can use a here document, using the <<
operator:
To read from a string, one can use a here string, using the <<<
operator:
or:
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file: