Go to the first, previous, next, last section, table of contents.


Input/Output Redirection

A command can have any of its input or output redirected by including an I/O directive. Each such directive is formed from an I/O word followed by a normal word and have meanings as described below. The leading n in the I/O word is an optional one or two digit number and, if present, specifies which I/O channel is being redirected. In each case, the second word is subjected to Variable, Arithmetic and Command substitution and, if a file name is expected, a limited form of Filename substitution in which the expansion must produce at most one file name or cause an error.

The I/O directives for any given command are processed in order so that if two directives attempt to redirect the same channel, the second one is effective.

`n< file'
(pronounced "come from file") The file is opened for reading and associated with channel 0 (Standard Input) by default. The file must already exist and be accessible by the user.
`n> file'
("output to file") The file is opened for writing, truncating it or creating it as necessary, and is associated with channel 1 (Standard Output) by default. The user must, naturally, have the required permissions.
`n<> file'
The file is opened for reading and writing and is created if necessary. By default it is associated with channel 0 (Standard Input).
`n>> file'
("append to file") The file is opened for writing in append mode and is created if necessary. The contents are not destroyed and all output occurs at the end of the file. The default channel is Standard Output.
`n<<[-] word'
("here file to word") following lines up to, but not including, the first line containing only that given word are read and stored in a temporary file. This file is then opened for input and given to the program on channel 0 (or n if given). If the hyphen (---) appears at the end of the I/O word, all leading tabs are stripped from the lines as they are read, thus allowing indenting in shell scripts. If any of the characters in the word are quoted, the here-file is passed through unchanged. Otherwise, dollar signs and back quotes which are not preceded by a slosh cause Variable, Arithmetic, or Command substitution as appropriate.
`n<& channel'
The channel given by the number channel is duplicated so that it is also associated with channel n (or 0 by default). If the channel given is not open, then n is closed.
`n>& channel'
Works identically to <& except that the default channel is 1.
`n<& ---'
`n>& ---'
The appropriate stream is closed.
`n<& [ expression]'
`n<& +[ expression]'
`n>& [ expression]'
`n>& +[ expression]'
This causes a seek to occur on the appropriate I/O stream. If there is a leading plus (+) then the seek is relative to the current file pointer, otherwise it is relative to the beginning of the file for non-negative offsets and from the end of the file for negative offsets.

Note that I/O redirection is done after pipes are set up, thus in the pipeline

prog1 2>&1 | prog2

the Standard Error as well as the Standard Output of prog1 will be sent into the pipe to prog2.

I/O directives for complex commands affect all sub-commands of that command unless, of course, the sub-command has an over-riding I/O directive.


Go to the first, previous, next, last section, table of contents.