-->

Previous | Table of Contents | Next

Page 22

The word that follows the redirection operator in the following descriptions is subjected to brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, quote removal, and pathname expansion. If it expands to more than one word, bash reports an error.

Note that the order of redirections is significant. For example, the command:


ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command


ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.

REDIRECTING OUTPUT

Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descriptor 0) if n is not specified.

The general format for redirecting input is


[n]<word

REDIRECTING OUTPUT

Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist, it is created; if it does exist it is truncated to zero size.

The general format for redirecting output is


[n]>word

If the redirection operator is >|, then the value of the -C option to the set built-in command is not tested, and file creation is attempted. (See also the description of noclobber under "Shell Variables," earlier in this manual page.)

APPENDING REDIRECTED OUTPUT

Redirection of output in this fashion causes the file whose name results from the expansion of word to be opened for appending on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist, it is created.

The general format for appending output is


[n]>>word

REDIRECTING STANDARD OUTPUT AND STANDARD ERROR

bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

There are two formats for redirecting standard output and standard error:


&>word

and


>&word

Of the two forms, the first is preferred. This is semantically equivalent to


>word 2>&1

HERE-DOCUMENTS

This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

Previous | Table of Contents | Next