这里会显示出您选择的修订版和当前版本之间的差别。
jaysnote:redirection [2018/12/11 16:00] jaylee |
jaysnote:redirection [2021/06/22 23:14] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | |||
- | # man bash redirection | ||
- | |||
- | ## REDIRECTION | ||
- | |||
- | Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. | ||
- | |||
- | Each redirection | ||
- | |||
- | In the following | ||
- | |||
- | The word following the redirection operator in the following descriptions, | ||
- | |||
- | Note that the order of redirections is significant. | ||
- | |||
- | ```bash | ||
- | ls > dirlist 2>&1 | ||
- | ``` | ||
- | directs both standard output and standard error to the file dirlist, while the command | ||
- | |||
- | ```bash | ||
- | ls 2>&1 > dirlist | ||
- | ``` | ||
- | directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard output was redirected to dirlist. | ||
- | |||
- | Bash handles several filenames specially when they are used in redirections, | ||
- | |||
- | * /dev/fd/fd | ||
- | If fd is a valid integer, file descriptor fd is duplicated. | ||
- | |||
- | * /dev/stdin | ||
- | File descriptor 0 is duplicated. | ||
- | * /dev/stdout | ||
- | File descriptor 1 is duplicated. | ||
- | * /dev/stderr | ||
- | File descriptor 2 is duplicated. | ||
- | * / | ||
- | If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding TCP socket. | ||
- | * / | ||
- | If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding UDP socket. | ||
- | |||
- | A failure to open or create a file causes the redirection to fail. | ||
- | |||
- | Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally. | ||
- | |||
- | Note that the exec builtin command can make redirections take effect in the current shell. | ||
- | |||
- | ### Redirecting Input | ||
- | |||
- | Redirection | ||
- | |||
- | The general format for redirecting input is: | ||
- | |||
- | ```bash | ||
- | [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 | ||
- | 1) if n is not specified. | ||
- | |||
- | The general format for redirecting output is: | ||
- | |||
- | ```bash | ||
- | [n]>word | ||
- | ``` | ||
- | If the redirection operator is >, and the `noclobber` option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is >|, or the redirection operator is > and the `noclobber`option | ||
- | |||
- | ### Appending Redirected Output | ||
- | |||
- | Redirection | ||
- | |||
- | The general format for appending output is: | ||
- | |||
- | ```bash | ||
- | [n]>> | ||
- | ``` | ||
- | ### Redirecting Standard Output and Standard Error | ||
- | |||
- | This construct 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. | ||
- | |||
- | There are two formats for redirecting standard output and standard error: | ||
- | |||
- | ```bash | ||
- | &> | ||
- | ``` | ||
- | and | ||
- | |||
- | ```bash | ||
- | >& | ||
- | ``` | ||
- | Of the two forms, the first is preferred. | ||
- | |||
- | ```bash | ||
- | >word 2>&1 | ||
- | ``` | ||
- | When using the second form, word may not expand to a number or `-`. If it does, other redirection operators apply (see [Duplicating File Descriptors below](Duplicating File Descriptors )) for compatibility reasons. | ||
- | |||
- | ### Appending Standard Output and Standard Error | ||
- | |||
- | This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion | ||
- | |||
- | The format for appending standard output and standard error is: | ||
- | |||
- | ```bash | ||
- | &>> | ||
- | ``` | ||
- | This is semantically equivalent to | ||
- | |||
- | ```bash | ||
- | >> | ||
- | ``` | ||
- | (see [Duplicating File Descriptors below](# | ||
- | |||
- | ### Here Documents | ||
- | |||
- | This type of redirection | ||
- | |||
- | The format of here-documents is: | ||
- | |||
- | ```bash | ||
- | << | ||
- | here-document | ||
- | delimiter | ||
- | ``` | ||
- | |||
- | No parameter and variable expansion, command substitution, | ||
- | |||
- | ### Here Strings | ||
- | |||
- | A variant of here documents, the format is: | ||
- | |||
- | ```bash | ||
- | <<< | ||
- | ``` | ||
- | The word undergoes | ||
- | |||
- | ### Duplicating File Descriptors | ||
- | |||
- | The redirection operator | ||
- | |||
- | ```bash | ||
- | [n]<& | ||
- | ``` | ||
- | is used to duplicate input file descriptors. | ||
- | The operator | ||
- | |||
- | ```bash | ||
- | [n]>& | ||
- | ``` | ||
- | is used similarly to duplicate output file descriptors. | ||
- | |||
- | ### Moving File Descriptors | ||
- | |||
- | The redirection operator | ||
- | |||
- | ```bash | ||
- | [n]<& | ||
- | ``` | ||
- | moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. | ||
- | |||
- | ```bash | ||
- | [n]>& | ||
- | ``` | ||
- | moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified. | ||
- | |||
- | ### Opening File Descriptors for Reading and Writing | ||
- | |||
- | The redirection operator | ||
- | |||
- | ```bash | ||
- | [n]<> | ||
- | ``` | ||
- | causes the file whose name is the expansion of word to be opened for both reading and writing on file descriptor n, or on file descriptor 0 if n is not specified. | ||
- | |||
- | </ | ||