This article is one of the seventh chapter Input/Output and Command-Line Processing of Learning the bash Shell 3rd Edition. We have learned the basic IO redirection operations of shell:>, <, and |. It can basically meet 95% of the conditions, but we need to know the redirection operations supported by bash.
- Cmd1
|Cmd2
: Pipe, SetCmd1
Standard outputCmd2
Standard input
- >File
: Redirects standard outputFile
- <File
: Use file as the standard input.
- >File
: Redirects the standard output to the file. If the file exists, append it to the file, that is, append it to the end of the file, rather than overwrite the file.
When cat does not include a parameter, it indicates that the standard input is used as the input. This allows you to type related content in the standard input. Add alias to. bashrc as the last line.
$ Cat>. bashrc
Alias cdmnt = 'mount-t iso9660/dev/sbpcd/cdrom'
^ D
- > |File
: Forcibly redirects standard output to file, even if noclobber is set. When the environment variable set-o noclobber is set, redirection to an existing file is prohibited to avoid overwriting the file.
- N
> |File
: Forcibly redirects file descriptor n to file, even if noclobber opens
- <>File
: Use file as the standard input and standard output. It is usually used for device files (files under/dev). The underlying system programmer can test the device driver, and others are rarely used.
- N
<>File
: Use file as the input and output of file descriptor n.
- <Label
: Here-document; see text. Use the standard shell input as the command input until the line contains the label. The input is here-document. The following is an example. We use cat> file to add content to the file through standard input.
$ Cat> msgfile <. # Here <. indicates that the end is. Therefore, you do not need to use ^ D instead.
> This is the text
> Our message.
>.
# This indicates the end. Then, two rows of this is… are added to msgfile... And our message.
MACHINE = "i586"
OS = "linux-gnu"
CC = "gcc"
Cat> $ file <
Machine: $ MACHINE
OS: $ OS
Compiler: $ CC
EOF
View: cat $ file. The normal structure is provided here.
Machine: i586
OS: linux-gnu
Compiler: gcc
If a single quotation mark is added to the EOF, or double quotation marks are as follows:
Cat> $ file <'eof'
The content of $ CC is not parsed. The file content is as follows:
Machine: $ MACHINE
OS: $ OS
Compiler: $ CC
If you use <-, as shown below, delete the> tab prefixed in all rows <, which is suitable for reading scripts.
For example, the preceding example can be written as follows:
Cat> $ file <-EOF
Machine: $ MACHINE
OS: $ OS
Compiler: $ CC
EOF
- N>File
: Redirects file descriptor n to file
- N
: Use file as the file descriptor input.
- N> file
: Redirects the output of file descriptor n to file. If file exists, append the output to the end of the file.
- N> &
: Copy standard output to file descriptor n (Duplicate standard output to file descriptor n)
- N <&
: Copy standard input from file descriptor n (Duplicate standard input from file descriptor n)
- N> & m
: File description n: copy a file to the description m (File descriptor n is made to be a copy of the output file descriptor)
- N <& m
: File description n is a copy of file description m (File descriptor n is made to be a copy of the input file descriptor)
- &> File
: Export standard output and standard error output to file
- <&-
: Disable Standard Input
- > &-
: Disable Standard output
- N> &-
: Close the output from file descriptor n)
- N <&-
: Close the input from file descriptor n)
- N> & word:
If n is not specified, the standard output (file descriptor 1) is used. if the digits in word do not specify a file descriptor open for output, a redirection error occurs. as a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previusly.
- N <& word
: If word expands to one or more digits, the file descriptor denoted by N is made to be a copy of that file descriptor. if the digits in word do not specify a file descriptor open for input, a redirection error occurs. if word evaluates to-, file descriptor N is closed. if n is not specified, the standard input (file descriptor 0) is used.
- N> & digit-
: Moves the file descriptor digit to file descriptor N, or the standard output (file descriptor 1) if n is not specified.
- N <& digit-
: Moves the file descriptor digit to file descriptor N, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
File descriptor
File descriptors are rarely used in bash. Data streams are represented by users starting from 0. 0 indicates standard input, 1 indicates standard output, 2 indicates incorrect output, and 3 indicates others. The most common scenario is to output error messages to a file. You can add2>File
To our command.
Let's take a look at the following script example:
Command
> Logfile 2> & 1 &
> Logfile, indicating that the standard output of command is redirected to the logfile, 2> & 1, matching n> & m, indicating file description 2 (standard error output of command) copy a copy using the file description 1 (standard output). Since the standard output has been redirected to the logfile, this copy will also be redirected to the file lofgile. We can use "abcd> logfile 2> & 1 &" to verify this effect. Last & indicates the background running mode. In this way, the command is run in the background, and its standard output and error output are redirected to the logfile file. Similar results can be achieved below:
Command
2> & 1 | tee logfile &
Standard output is also applicable to error output. In pipe mode, you can see that they are used as input to execute tee logfile. The tee Command copies its standard input to the files included in its standard output and parameters. Unlike the above commandStdout
And logfile.
Redirection of descriptions in other files, such as <& n, is usually used to read or write from multiple files.
<&-
, Indicates that the standard input is forcibly disabled.
> &-
To force the standard output off.
1>
, Equivalent to>
0 <
, Equivalent to <
Related Links: My articles on Linux operations