Linux-linux input and output redirection

 

1. Redirection command list

command Description
command > file Redirect the output to file.
command < file Redirect input to file.
command >> file Redirect the output to file by appending.
n > file Redirect the file with file descriptor n to file.
n >> file Redirect the file with file descriptor n to file by appending.
n >& m Combine the output files m and n.
n <& m Combine the input files m and n.
<< tag Take the content between the opening tag tag and the closing tag tag as input.

Note that file descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

 

2. Redirection in-depth explanation

Under normal circumstances, each Unix/Linux command will open three files when running:

  • Standard input file (stdin): The file descriptor of stdin is 0, and Unix programs read data from stdin by default.
  • Standard output file (stdout): The file descriptor of stdout is 1, and Unix programs output data to stdout by default.
  • Standard error file (stderr): The file descriptor of stderr is 2, and Unix programs will write error information to the stderr stream.

(1) stderr redirects to file

$ command 2>file

stderr is appended to the end of the file

$ command 2>>file

 

(2) Redirect to file after stdout and stderr merge

$ command > file 2>&1  或 $ command >> file 2>&1

 

(3) Redirect stdin to file1 and stdout to file2

$ command < file1 >file2

 

Note: 0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

There can be no space between 2 and> here. Only when 2> is one part, it means error output.

 

3./dev/null file

Do not display output results on the screen

$ command > /dev/null

 

Block stdout and stderr

$ command > /dev/null 2>&1

 

 

 

 

Guess you like

Origin blog.csdn.net/helunqu2017/article/details/113822715
Recommended