shell - (ten) Shell input / output redirection

Original: https://www.runoob.com/linux/linux-shell-io-redirections.html

Most UNIX systems command accepts input and output generated from your terminal sends back to your terminal. A command normally read input from a place called standard input by default, which happens to be your terminal. Similarly, a command is usually writes its output to standard output, by default, this is your terminal.

Redirect command list is as follows

command Explanation
command > file Redirect the output to a file.
command < file Redirect input to file.
command >> file The additional output redirected to the way file.
n > file The file descriptor n file redirected to file.
n >> file The file descriptor n to file additional way redirected to file.
n >& m M and n are the combined output file.
n <& m M and n are the combined input file.
<< tag It will mark the beginning of the content between the tag and the end tag tag as input.

0 Note that the file descriptor is typically standard input (STDIN), 1 is a standard output (STDOUT), 2 is the standard error output (STDERR).

Redirect depth explanation

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

  • Standard input file (stdin): stdin file descriptor is 0, Unix default program data read from stdin.
  • Standard output file (stdout): stdout file descriptor 1, Unix program default data output to stdout.
  • The standard error file (stderr): stderr file descriptor 2, Unix program writes error information to stderr stream.

By default, command> file redirect stdout to the file, command <file to redirect stdin to file.

If you want to redirect stderr to file, you can write:

$ command 2 > file

 

If you want to append to the end of file stderr file can be written //  2  standard error file (stderr)

$ command 2 >> file

 

If you want to merge after the stdout and stderr redirected to a file, you can write:

$ command > file 2>&1

或者

$ command >> file 2>&1

 

If you want to redirect stdin and stdout, you can write:

$ command < file1 >file2

command command stdin redirected to file1, redirect stdout to file2.

Here Document

Here Document Shell is in a special manner redirection, to redirect input to an interactive Shell scripts or programs.

Its basic form is as follows:

command << delimiter
    document
delimiter

It is the role of the content between the two delimiter (document) is transmitted as input to the command.

note:

  • ending delimiter must be the top grid write, can not have any characters in front of the back can not have any characters, including spaces and tab indent.
  • Space before and after the start of the delimiter is ignored.

Examples

The number of lines in the command line by calculating Here Document wc -l command:

$ WC the -l << EOF 
    Welcome to the 
    rookie tutorial 
    www.runoob.com 
EOF 
3           # output is 3 lines 
$

 

We can also Here Document used in a script, for example:

! # / bin / bash 
# author: rookie Tutorial 
# url: www.runoob.com 

CAT << EOF 
Welcome to the 
rookie tutorial 
www.runoob.com 
EOF

The implementation of the above script, output:

Welcome to the 
rookie tutorial 
www.runoob.com

/ Dev / null file

If you want to execute a command, but do not 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, its contents will be written to be discarded; if you try to read from the file, then nothing can not read. However, / dev / null file is very useful to redirect the output of a command to it, it will play the effect of "No Output".

If you want to shield stdout and stderr, can be written:

$ command > /dev/null 2>&1

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

Guess you like

Origin www.cnblogs.com/zhzhlong/p/12549696.html