I/O redirection is a process that captures the output of a file, or command, or program, or script, or even code block, and then sends the captured output as input to another file, or command, or program, or script.
1. I/O redirection conforms to the two categories of basic I/O redirection symbols and advanced I/O redirection symbols.
Basic I/O redirection symbols (note that you can use the FD file descriptor as the file name): CMD1 | CMD2: Pipe break, CMD1 standard output as standard input for CMD2
> FileName: Write standard output to file filename
< filename: Reads the contents of the file filename into the standard input
>> FileName: Writes the standard output to the file filename, and appends the standard output to the existing content of filename if the filename file already exists
>| FileName: Even if the noclobber option is turned on, the standard output is still forced to be written to the file filename and the filename file will be overwritten
n>| FileName: Even if the noclobber option is turned on, forcing the output of FD to N to be written to the file filename will overwrite the filename file
n> FileName: Writes the output of FD N to the file filename
n< FileName: Reads the contents of the file filename into fd n
n>> FileName: Writes the output of FD = N to the file filename, and if the filename file already exists, appends the output of FD to N to the existing content of filename
<<delimiter: Documentation Here (here-document)
Advanced I/O redirection symbols
N>&m: Copying the output of FD to M to a file with fd N
N<&m: Copying the input of FD to M to a file with fd N
n>&-: Turn off the output of FD to n
n<&-: Turn off the input for FD n
&>file: Redirecting standard output and standard error output to a file
--------the difference between I/O redirection and piping
The first thing to note is that the pipeline | is actually an operational part of I/O redirection
1. The left side of the pipeline is a command that can be sent to standard output (stdout), and the right side is a command that accepts (STDIN) from the standard input, assuming that the I/O redirector has an output command on the left, and that the right side is just a file, assuming that the I/O redirector is on the left side of the command that
2. When the pipeline is running, two processes are started, and the process runs both left and right commands, while the I/O redirection value is completed in the same process.
---------has two important special files in Linux:
/dev/null: This is an empty device file, all the data sent to it will be discarded, the standard output and error output can be redirected to the file, at this time, both the error message can be discarded.
Standard notation is >/dev/null 2>&1
Among them, >/dev/null means discard files, 2>&1 2 and 1 respectively indicate standard output stdout and standard error stderr,>& means equal, that is, the standard error output 2 is redirected to the callout output 1, Because the standard output has been relocated to/dev/null, the standard error output is also thrown away.
This writing can also be 1>/dev/null 2>/dev/null, the difference in such cases standard output and standard error is sent to/dev/null, the file was opened two times, and >/dev/null 2 >& 1 is to send the standard output to/dev/null, and the standard error is sent from the pipeline to/dev/null,/dev/null just opened once, in which/dev/null can be replaced by any other files.
/dev/tty: When the file is opened, the shell redirects itself to a terminal (including the display console, a remote login pseudo-interrupt, or a serial port).
46 I/O redirection for Linux