Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

I/O redirection

&pagelevel(4)&pagelevel

Before a command is executed, its input and output may be redirected by means of a special notation interpreted by the POSIX shell. The following arguments may appear anywhere in a simple command or may precede or follow a command. They are not passed on to the invoked command, but are interpreted by the shell. Command and parameter substitution occurs before the specified file or file descriptor is used. File name generation is performed only if the pattern matches a single file. Blank interpretation is not performed

<file

Redirects the standard input (file descriptor 0) of a command to file, so that input to the command is read from file.

>file

Redirects the standard output (file descriptor 1) of a command to file, so that output from the command is written to file. If the file does not exist, it is created. If it does exist, is a regular file, and the noclobber option is set (see Built-in commands, set -o), this causes an error. If noclobber is not set, the file is truncated to zero length and its existing contents are lost.

>|file

Same as >file, except that it overrides the noclobber option.

>>file

Redirects the standard output (file descriptor 1) of a command to file, so that output from the command is written to file. If the file exists, output is appended to it; otherwise, the file is created.

<>file

Opens file for reading and writing as standard input.

<<[-]string

Introduces a "here document" (explained below). "Here documents" are used primarily in shell procedures to supply values to commands which can read from standard input.No parameter substitution, command substitution or file name generation is performed on string. Input to the POSIX shell is read up to (but excluding) a line that literally matches string or to an end-of-file. The resulting document, called a "here document", becomes the standard input for the command.

If one of the characters in string is quoted then all the characters of the "here document" are quoted for the POSIX shell.


The string marking the last line must not be quoted.


If no characters of string are quoted:

  • parameter and command substitution is performed,

  • escaped newlines (\newline) are ignored, and

  • a backslash (\) must be used to quote backslashes (\), dollar signs ($), backquotes (`) and the first character of string if they are to be treated literally in the text.

If - is appended to <<, then all leading tabs are stripped from string and from each line in the here document.

<&digit
>&digit

In the first form, the standard input is duplicated from file descriptor digit, i.e. is read from the file associated with digit. The second form does the same for the standard output.

<&-
>&-

The first form closes the standard input for the command, so that EOF is the only input to the command. The second form closes the standard output for the command, so that the command does not write any output.

<&p
>&p

The input from the co-process in a two-way pipeline is redirected to standard input. Similarly, the output to the co-process is redirected to standard output.


If one of the above redirections is preceded by a digit, the file descriptor number referred to is that specified by the digit (instead of the default 0 for stdin and 1 for stdout).

Example

To open file descriptor 2 (stderr) for writing as a duplicate of file descriptor 1 (stdout):

... 2>&1

Order of redirections and background commands

The order in which redirections are specified is significant. The POSIX shell evaluates each redirection in terms of the association (file descriptor, file) at the time of evaluation.

For example:

... 1>file 2>&1

first associates standard output (file descriptor 1) with file and then associates standard error (file descriptor 2) with the file associated with file descriptor 1 (i.e. file). In other words, both the standard output and the error messages from the command are written to file.
If the order were reversed, file descriptor 2 would be associated with the terminal (assuming file descriptor 1 had been), file descriptor 1 with file. This would mean that only the standard output would be written to file, not the error messages.

If you use & to start a command in the background without active job control then the standard input is automatically associated with a file which possesses the same properties as /dev/null. If job control is active then the environment for command execution contains the same file descriptors, modified as appropriate by the input/output statements, as the executing POSIX shell.

You can set commands or pipelines to run in the background from within a script. They can then communicate with your program. To spawn a co-process of this type, you put the operator |& after the command. You can only use two-way pipes in scripts, not on the command line.

Command strings can be called as two-way pipes. If you abort the original process (e.g. with kill -9 PID) and then later attempt to write another command to a two-way pipe then a subshell is indeed called but the process is stopped. You receive the error message:

sh: bad file unit number.