bash: Redirections
1
1 3.6 Redirections
1 ================
1
1 Before a command is executed, its input and output may be REDIRECTED
1 using a special notation interpreted by the shell. Redirection allows
1 commands' file handles to be duplicated, opened, closed, made to refer
1 to different files, and can change the files the command reads from and
1 writes to. Redirection may also be used to modify file handles in the
1 current shell execution environment. The following redirection
1 operators may precede or appear anywhere within a simple command or may
1 follow a command. Redirections are processed in the order they appear,
1 from left to right.
1
1 Each redirection that may be preceded by a file descriptor number may
1 instead be preceded by a word of the form {VARNAME}. In this case, for
1 each redirection operator except >&- and <&-, the shell will allocate a
1 file descriptor greater than 10 and assign it to {VARNAME}. If >&- or
1 <&- is preceded by {VARNAME}, the value of VARNAME defines the file
1 descriptor to close.
1
1 In the following descriptions, if the file descriptor number is
1 omitted, and the first character of the redirection operator is '<', the
1 redirection refers to the standard input (file descriptor 0). If the
1 first character of the redirection operator is '>', the redirection
1 refers to the standard output (file descriptor 1).
1
1 The word following the redirection operator in the following
1 descriptions, unless otherwise noted, is subjected to brace expansion,
1 tilde expansion, parameter expansion, command substitution, arithmetic
1 expansion, quote removal, filename expansion, and word splitting. If it
1 expands to more than one word, Bash reports an error.
1
1 Note that the order of redirections is significant. For example, the
1 command
1 ls > DIRLIST 2>&1
1 directs both standard output (file descriptor 1) and standard error
1 (file descriptor 2) to the file DIRLIST, while the command
1 ls 2>&1 > DIRLIST
1 directs only the standard output to file DIRLIST, because the standard
1 error was made a copy of the standard output before the standard output
1 was redirected to DIRLIST.
1
1 Bash handles several filenames specially when they are used in
1 redirections, as described in the following table. If the operating
1 system on which Bash is running provides these special files, bash will
1 use them; otherwise it will emulate them internally with the behavior
1 described below.
1
1 '/dev/fd/FD'
1 If FD is a valid integer, file descriptor FD is duplicated.
1
1 '/dev/stdin'
1 File descriptor 0 is duplicated.
1
1 '/dev/stdout'
1 File descriptor 1 is duplicated.
1
1 '/dev/stderr'
1 File descriptor 2 is duplicated.
1
1 '/dev/tcp/HOST/PORT'
1 If HOST is a valid hostname or Internet address, and PORT is an
1 integer port number or service name, Bash attempts to open the
1 corresponding TCP socket.
1
1 '/dev/udp/HOST/PORT'
1 If HOST is a valid hostname or Internet address, and PORT is an
1 integer port number or service name, Bash attempts to open the
1 corresponding UDP socket.
1
1 A failure to open or create a file causes the redirection to fail.
1
1 Redirections using file descriptors greater than 9 should be used
1 with care, as they may conflict with file descriptors the shell uses
1 internally.
1
1 3.6.1 Redirecting Input
1 -----------------------
1
1 Redirection of input causes the file whose name results from the
1 expansion of WORD to be opened for reading on file descriptor 'n', or
1 the standard input (file descriptor 0) if 'n' is not specified.
1
1 The general format for redirecting input is:
1 [N]<WORD
1
1 3.6.2 Redirecting Output
1 ------------------------
1
1 Redirection of output causes the file whose name results from the
1 expansion of WORD to be opened for writing on file descriptor N, or the
1 standard output (file descriptor 1) if N is not specified. If the file
1 does not exist it is created; if it does exist it is truncated to zero
1 size.
1
1 The general format for redirecting output is:
1 [N]>[|]WORD
1
1 If the redirection operator is '>', and the 'noclobber' option to the
1 'set' builtin has been enabled, the redirection will fail if the file
1 whose name results from the expansion of WORD exists and is a regular
1 file. If the redirection operator is '>|', or the redirection operator
1 is '>' and the 'noclobber' option is not enabled, the redirection is
1 attempted even if the file named by WORD exists.
1
1 3.6.3 Appending Redirected Output
1 ---------------------------------
1
1 Redirection of output in this fashion causes the file whose name results
1 from the expansion of WORD to be opened for appending on file descriptor
1 N, or the standard output (file descriptor 1) if N is not specified. If
1 the file does not exist it is created.
1
1 The general format for appending output is:
1 [N]>>WORD
1
1 3.6.4 Redirecting Standard Output and Standard Error
1 ----------------------------------------------------
1
1 This construct allows both the standard output (file descriptor 1) and
1 the standard error output (file descriptor 2) to be redirected to the
1 file whose name is the expansion of WORD.
1
1 There are two formats for redirecting standard output and standard
1 error:
1 &>WORD
1 and
1 >&WORD
1 Of the two forms, the first is preferred. This is semantically
1 equivalent to
1 >WORD 2>&1
1 When using the second form, WORD may not expand to a number or '-'.
1 If it does, other redirection operators apply (see Duplicating File
1 Descriptors below) for compatibility reasons.
1
1 3.6.5 Appending Standard Output and Standard Error
1 --------------------------------------------------
1
1 This construct allows both the standard output (file descriptor 1) and
1 the standard error output (file descriptor 2) to be appended to the file
1 whose name is the expansion of WORD.
1
1 The format for appending standard output and standard error is:
1 &>>WORD
1 This is semantically equivalent to
1 >>WORD 2>&1
1 (see Duplicating File Descriptors below).
1
1 3.6.6 Here Documents
1 --------------------
1
1 This type of redirection instructs the shell to read input from the
1 current source until a line containing only WORD (with no trailing
1 blanks) is seen. All of the lines read up to that point are then used
1 as the standard input (or file descriptor N if N is specified) for a
1 command.
1
1 The format of here-documents is:
1 [N]<<[-]WORD
1 HERE-DOCUMENT
1 DELIMITER
1
1 No parameter and variable expansion, command substitution, arithmetic
1 expansion, or filename expansion is performed on WORD. If any part of
1 WORD is quoted, the DELIMITER is the result of quote removal on WORD,
1 and the lines in the here-document are not expanded. If WORD is
1 unquoted, all lines of the here-document are subjected to parameter
1 expansion, command substitution, and arithmetic expansion, the character
1 sequence '\newline' is ignored, and '\' must be used to quote the
1 characters '\', '$', and '`'.
1
1 If the redirection operator is '<<-', then all leading tab characters
1 are stripped from input lines and the line containing DELIMITER. This
1 allows here-documents within shell scripts to be indented in a natural
1 fashion.
1
1 3.6.7 Here Strings
1 ------------------
1
1 A variant of here documents, the format is:
1 [N]<<< WORD
1
1 The WORD undergoes brace expansion, tilde expansion, parameter and
1 variable expansion, command substitution, arithmetic expansion, and
1 quote removal. Pathname expansion and word splitting are not performed.
1 The result is supplied as a single string, with a newline appended, to
1 the command on its standard input (or file descriptor N if N is
1 specified).
1
1 3.6.8 Duplicating File Descriptors
1 ----------------------------------
1
1 The redirection operator
1 [N]<&WORD
1 is used to duplicate input file descriptors. If WORD expands to one or
1 more digits, the file descriptor denoted by N is made to be a copy of
1 that file descriptor. If the digits in WORD do not specify a file
1 descriptor open for input, a redirection error occurs. If WORD
1 evaluates to '-', file descriptor N is closed. If N is not specified,
1 the standard input (file descriptor 0) is used.
1
1 The operator
1 [N]>&WORD
1 is used similarly to duplicate output file descriptors. If N is not
1 specified, the standard output (file descriptor 1) is used. If the
1 digits in WORD do not specify a file descriptor open for output, a
1 redirection error occurs. If WORD evaluates to '-', file descriptor N
1 is closed. As a special case, if N is omitted, and WORD does not expand
1 to one or more digits or '-', the standard output and standard error are
1 redirected as described previously.
1
1 3.6.9 Moving File Descriptors
1 -----------------------------
1
1 The redirection operator
1 [N]<&DIGIT-
1 moves the file descriptor DIGIT to file descriptor N, or the standard
1 input (file descriptor 0) if N is not specified. DIGIT is closed after
1 being duplicated to N.
1
1 Similarly, the redirection operator
1 [N]>&DIGIT-
1 moves the file descriptor DIGIT to file descriptor N, or the standard
1 output (file descriptor 1) if N is not specified.
1
1 3.6.10 Opening File Descriptors for Reading and Writing
1 -------------------------------------------------------
1
1 The redirection operator
1 [N]<>WORD
1 causes the file whose name is the expansion of WORD to be opened for
1 both reading and writing on file descriptor N, or on file descriptor 0
1 if N is not specified. If the file does not exist, it is created.
1