HOME

TheInfoList



OR:

In computing, redirection is a form of
interprocess communication In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categor ...
, and is a function common to most
command-line interpreter A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pr ...
s, including the various
Unix shell A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to ...
s that can redirect
standard streams In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (std ...
to user-specified locations. In Unix-like operating systems, programs do redirection with the
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, ac ...
, or its less-flexible but higher-level
stdio The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header . The functionality descends from a "portable I/O package" written by Mike Lesk at ...
analogues, and .


Redirecting standard input and standard output

Redirection is usually implemented by placing certain
characters Character or Characters may refer to: Arts, entertainment, and media Literature * ''Character'' (novel), a 1936 Dutch novel by Ferdinand Bordewijk * ''Characters'' (Theophrastus), a classical Greek set of character sketches attributed to The ...
between commands.


Basic

Typically, the
syntax In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituency ...
of these characters is as follows, using < to redirect input, and > to redirect output. command > file1 executes , placing the output in , as opposed to displaying it at the terminal, which is the usual destination for standard output. This will clobber any existing data in . Using command < file1 executes , with as the source of input, as opposed to the
keyboard Keyboard may refer to: Text input * Keyboard, part of a typewriter * Computer keyboard ** Keyboard layout, the software control of computer keyboards and their mapping ** Keyboard technology, computer keyboard hardware and firmware Music * Mus ...
, which is the usual source for standard input. command < infile > outfile combines the two capabilities: reads from and writes to


Variants

To append output to the end of the file, rather than clobbering it, the >> operator is used: command1 >> file1. To read from a stream literal (an inline file, passed to the standard input), one can use a
here document In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also ...
, using the << operator: $ tr a-z A-Z << END_TEXT > one two three > uno dos tres > END_TEXT ONE TWO THREE UNO DOS TRES To read from a string, one can use a here string, using the <<< operator: tr a-z A-Z <<< "one two three", or: $ NUMBERS="one two three" $ tr a-z A-Z <<< "$NUMBERS" ONE TWO THREE


Piping

Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file. command1 , command2 executes , using its output as the input for (commonly called
piping Within industry, piping is a system of pipes used to convey fluids (liquids and gases) from one location to another. The engineering discipline of piping design studies the efficient transport of fluid. Industrial process piping (and accompan ...
, with the ", " 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 Synthesis or synthesize may refer to: Science Chemistry and biochemistry *Chemical synthesis, the execution of chemical reactions to form a more complex molecule from chemical precursors **Organic synthesis, the chemical synthesis of organi ...
, 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: $ command1 > tempfile $ command2 < tempfile $ rm tempfile But here, does not start executing until 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
echo In audio signal processing and acoustics, an echo is a reflection of sound that arrives at the listener with a delay after the direct sound. The delay is directly proportional to the distance of the reflecting surface from the source and the lis ...
with another command to achieve something interactive in a non-interactive shell, e.g. echo -e 'user\npass' , ftp localhost. This runs the
ftp The File Transfer Protocol (FTP) is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network. FTP is built on a client–server model architecture using separate control and data ...
client with input , press , then . In casual use, the initial step of a pipeline is often cat or echo, 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 cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files (from Latin ''catenare'', "to chain"). It has been ported to a number of operating syste ...
. For example, the following commands: $ cat infile , command $ echo $string , command $ echo -e 'user\npass' , ftp localhost can be replaced by: $ command < infile $ command <<< $string $ ftp localhost <<< $'user\npass' As echo 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 A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to ...
s derived from the original
Bourne shell The Bourne shell (sh) is a shell command-line interpreter for computer operating systems. The Bourne shell was the default shell for Version 7 Unix. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic ...
, the first two actions can be further modified by placing a number (the
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically hav ...
) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are: For example, executes , directing the
standard error The standard error (SE) of a statistic (usually an estimate of a parameter) is the standard deviation of its sampling distribution or an estimate of that standard deviation. If the statistic is the sample mean, it is called the standard error of ...
stream to . In shells derived from csh (the
C shell The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
), the syntax instead appends the (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. vs . In the first case, stderr is redirected to a file named '' 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 error The standard error (SE) of a statistic (usually an estimate of a parameter) is the standard deviation of its sampling distribution or an estimate of that standard deviation. If the statistic is the sample mean, it is called the standard error of ...
into
standard output In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdi ...
so error messages can be processed together with (or alternately to) the usual output. For example, will try to find all files named . Executed without redirection, it will output hits to
stdout In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdi ...
and errors (e.g. for lack of privilege to traverse protected directories) to
stderr In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
. If standard output is directed to file , error messages appear on the console. To see both hits and error messages in file , merge
stderr In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
(handle 2) into
stdout In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdi ...
(handle 1) using . If the merged output is to be piped into another program, the file merge sequence must precede the pipe symbol, thus, A simplified but non-POSIX conforming form of the command, is (not available in Bourne Shell prior to version 4, final release, or in the standard shell
Debian Almquist shell Almquist shell (also known as A Shell, ash and sh) is a lightweight Unix shell originally written by Kenneth Almquist in the late 1980s. Initially a clone of the System V.4 variant of the Bourne shell, it replaced the original Bourne shell in th ...
used in Debian/Ubuntu): or . It is possible to use 2>&1 before ">" but the result is commonly misunderstood. The rule is that any redirection sets the handle to the output stream independently. So "2>&1" sets handle 2 to whatever handle 1 points to, which at that point usually is ''stdout''. Then ">" redirects handle 1 to something else, e.g. a file, but it does not change handle 2, 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: . 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: .


Chained pipelines

The redirection and piping tokens can be chained together to create complex commands. For example, sort infile , uniq -c , sort -n > outfile sorts the lines of in lexicographical order, writes unique lines prefixed by the number of occurrences, sorts the resultant output numerically, and places the final output in . This type of construction is used very commonly in
shell script A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file mani ...
s and
batch file Batch may refer to: Food and drink * Batch (alcohol), an alcoholic fruit beverage * Batch loaf, a type of bread popular in Ireland * A dialect term for a bread roll used in North Warwickshire, Nuneaton and Coventry, as well as on the Wirral ...
s.


Redirect to multiple outputs

The standard command can redirect output from a command to several destinations:ls -lrt , tee xyz. This directs the file list output to both standard output and the file .


See also

* Here-document, a way of specifying text for input in command-line shells * Shell shoveling * Command substitution *
Process substitution In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell. This all ...


External links

*
Redirection Definition
by The Linux Information Project (LINFO)

i
The Linux Documentation ProjectRedirection in WindowsCreating a Child Process with Redirected Input and Output
in Windows {{DEFAULTSORT:Redirection (Computing) Articles with example code DOS technology Unix software Unix Windows administration pl:Standardowe strumienie