zsh - Redirection

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

# Redirection

Before a command is executed, its input and output may be redirected. The following may appear anywhere in a simple command or may precede or follow a complex command. Substitution occurs before word or digit is used except as noted below. If the result of substitution on word produces more than one filename, redirection occurs for each separate filename in turn.

`<word`
  Open file word as standard input.
`<>word`
  Open file word for reading and writing as standard input. If the file does not exist then it is created.
`>word`
  Open file word as standard output. If the file does not exist then it is created. If the file exists, and the `CLOBBER` option is unset, this causes an error; otherwise, it is truncated to zero length.
`>|word`
`>!word`
  Same as `>`, except that the file is truncated to zero length if it exists, even if `CLOBBER` is unset.
`>>word`
  Open file word as standard output. If the file exists then output is appended to it. If the file does not exist, and the `CLOBBER` option is unset, this causes an error; otherwise, the file is created.
`>>|word`
`>>!word`
  Same as `>>`, except that the file is created if it does not exist, even if `CLOBBER` is unset.
`<<[-]word`
  The shell input is read up to a line that is the same as word, or to an end-of-file. No parameter substitution, command substitution or filename generation is performed on word. The resulting document, called a here-document, becomes the standard input. If any character of word is quoted with single or double quotes or a `\`, no interpretation is placed upon the characters of the document. Otherwise, parameter and command substitution occurs, `\` followed by a newline is removed, and `\` must be used to quote the characters `\`, `$`, ```, and the first character of word. If `<<-` is used, then all leading tabs are stripped from word and from the document.
`<<<word`
  Perform shell expansion on word and pass the result to standard input.
`<&digit`
  The standard input is duplicated from file descriptor digit (see `dup(2)`). Similarly for standard output using `>&digit`.
`>&word`
  Same as `>word 2>&1`.
`>>&word`
  Same as `>>word 2>&1`.
`<&-`
  Close the standard input.
`>&-`
  Close the standard output.
`<&p`
  The input from the coprocess is moved to the standard input.
`>&p`
  The output to the coprocess is moved to the standard output.

If one of the above is preceded by a digit, then the file descriptor referred to is that specified by the digit (instead of the default 0 or 1). The order in which redirections are specified is significant. The shell evaluates each redirection in terms of the (file descriptor, file) association at the time of evaluation. For example:

`... 1>fname 2>&1`

first associates file descriptor 1 with file fname. It then associates file descriptor 2 with the file associated with file descriptor 1 (that is, fname). If the order of redirections were reversed, file descriptor 2 would be associated with the terminal (assuming file descriptor 1 had been) and then file descriptor 1 would be associated with file fname.

If the user tries to open a file descriptor for writing more than once, the shell opens the file descriptor as a pipe to a process that copies its input to all the specified outputs, similar to `tee(1)`, provided the `MULTIOS` option is set. Thus:

`date >foo >bar`

writes the date to two files, named ``foo'` and ``bar'`. Note that a pipe is an implicit indirection; thus

`date >foo | cat`

writes the date to the file ``foo'`, and also pipes it to `cat`.

If the `MULTIOS` option is set, the word after a redirection operator is also subjected to filename generation (globbing). Thus

`: > *`

will truncate all files in the current directory, assuming there's at least one. (Without the `MULTIOS` option, it would create an empty file called ``*'`.)

If the user tries to open a file descriptor for reading more than once, the shell opens the file descriptor as a pipe to a process that copies all the specified inputs to its output in the order specified, similar to `cat(1)`, provided the `MULTIOS` option is set. Thus

`sort <foo <fubar`

or even

`sort <f{oo,ubar}`

is equivalent to ``cat foo fubar | sort'`. Similarly, you can do

`echo exit 0 >> *.sh`

Note that a pipe is an implicit indirection; thus

`cat bar | sort <foo`

is equivalent to ``cat bar foo | sort'` (note the order of the inputs).

If the `MULTIOS` option is unset, each redirection replaces the previous redirection for that file descriptor. However, all files redirected to are actually opened, so

`echo foo > bar > baz`

when `MULTIOS` is unset will truncate `bar`, and write `foo` into `baz`.

If a simple command consists of one or more redirection operators and zero or more parameter assignments, but no command name, the command `cat` is assumed. Thus

`< file`

prints the contents of `file`.

If a command is followed by `&` and job control is not active, then the default standard input for the command is the empty file ``/dev/null'`. Otherwise, the environment for the execution of a command contains the file descriptors of the invoking shell as modified by input/output specifications.

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