Shell input/Output redirection
The Unix system command accepts input from the terminal and sends the resulting output back to the terminal. The command reads the input and becomes the standard input. The default is the terminal; the output of the command is the standard output, and the default is also the terminal.
- Command > file redirects the output to file.
- Command < file redirects the input to file.
- Command >> file redirects the output to file in an append manner.
- n > file redirects the file descriptor n files to filename.
- n >> files redirect files with file descriptor N as an append to file.
- N >& m merges the output file m and N.
- N <& m merges the input file m and N.
- << tag will start to tag the contents of tags between tag and end tag as input.
Note: The file descriptor 0 is the standard input stdin,1 is the standard output stdout,2 is the standard error stderr
Output from redirect
* command1 > file1, if top>text.txt is executed, the result will be saved to the text.txt file instead of being output to the terminal.
* command1 >> file1 , do not want the file memory to be overwritten, you can use >> to append the result of command2 to file1 file
Input redirect
-
Command1 < file1 getting input from a file
Command1 < infile > outfile
Replace input and output at the same time, execute Command1, read content from File infile, and write input to outfile
Redirect in-depth explanation
- Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
- Standard output file (stdout): StdOut file descriptor for 1,unix program outputs data to stdout by default.
-
Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.
Command > file redirect standard output to file
Command < file redirect standard input to file
Command 2 > file, command 2 >> file if you want the standard error to be redirected to file
If you want to redirect standard output and standard errors to file, command > File 2 > &1,command >> file 2 >&1
Here Document Here Document is a special way of redirecting in the shell to redirect input to an interactive shell script or program. The form is:
#shellcommand << delimiter documentdelimiter
Its role is to pass the contents (document) between the two delimiter as input to the command.
Attention:
* At the end of the delimiter must be shelf write, the front can not have any characters, behind can not have any characters, including spaces and tab indentation.
* The space before and after the beginning of the delimiter is ignored.
#shell#!/bin/bashwc -l << EOF hello world hello java hello rubyEOF
3
/dev/null file
If you want to execute a command but don't want to display the output on the screen, you can redirect the output to /dev/null:command > /dev/null/dev/null is a special file that is written to it The content will be discarded; if you try to read the content from the file, nothing can be read. But the /dev/null file is very useful, redirecting the output of the command to it will have the effect of "forbidden output".
If you want to block stdout and stderr, you can write this:
Command >/dev/null 2>&1
&
Command > File 2>&1
& on the back of >, which indicates that the redirect target is not a file, but a file descriptor, 2>1 represents redirecting a standard error to a file with a file descriptor of 1
Shell 12 input and output redirection