Shell input/output redirection


Most UNIX system commands accept input from your terminal and send the resulting output back to your terminal. A command usually reads input from a place called standard input, which happens to be your terminal by default. Likewise, a command usually writes its output to standard output, which is also your terminal by default.

The redirection command list is as follows:

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

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


Output redirection

Redirection is generally achieved by inserting specific symbols between commands. In particular, the syntax of these symbols is as follows:

command1 > file1

The above command executes command1 and then stores the output content in file1.

Note that any existing content in file1 will be replaced by the new content. If you want to add new content at the end of the file, use the >> operator.

Example

Execute the following who command, which will redirect the complete output of the command in the user file (users):

$ who > users

After execution, there is no terminal Output information because output has been redirected from the default standard output device (terminal) to the specified file.

You can use the cat command to view the file content:

$ cat users _mbsetupuser console Oct 31 17:35 tianqixin console Oct 31 17:35 tianqixin ttys000 Dec 1 11:33

Output redirection will overwrite the file content, please see the following example:

$ echo "php中文网:m.sbmmt.com" > users $ cat users php中文网:m.sbmmt.com $

If you do not want the file content to be overwritten , you can use >> to append to the end of the file, for example:

$ echo "php中文网:m.sbmmt.com" >> users $ cat users php中文网:m.sbmmt.com php中文网:m.sbmmt.com $

Input redirection

Like output redirection, Unix commands can also get input from files, the syntax is :

command1 < file1

In this way, commands that originally require input from the keyboard will be transferred to file reading content.

Note: Output redirection is greater than sign (>), input redirection is less than sign (<).

Example

Following the above example, we need to count the number of lines in the users file and execute the following command:

$ wc -l users 2 users

You can also redirect the input to the users file:

$ wc -l < users 2

Note: The results of the above two examples are different: the first example will output the file name; the second one will not, because it only knows to read content from the standard input.

command1 < infile > outfile

Replace input and output at the same time, execute command1, read the content from the file infile, and then write the output to outfile.

In-depth explanation of redirection

Generally, three files will be opened when each Unix/Linux command is run:

  • 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. 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.

By default, command > file redirects stdout to file, and command < file redirects stdin to file.

If you want stderr to be redirected to file, you can write like this:

$ command 2 > file

If you want stderr to be appended to the end of file, you can write like this:

$ command 2 >> file

2represents the standard error file (stderr).

If you want to merge stdout and stderr and redirect them to file, you can write like this:

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

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

$ command < file1 >file2
## The #command command redirects stdin to file1 and stdout to file2.


Here Document

Here Document is a special redirection method in Shell, used to redirect input to an interactive Shell script or program.

Its basic form is as follows:

command << delimiter document delimiter

Its function is to pass the content (document) between two delimiters as input to command.

Notice:

  • The delimiter at the end must be written in the top format. There cannot be any characters in front of it, nor can there be any characters in the back, including spaces and tab indents.

  • The spaces before and after the initial delimiter will be ignored.

Example

Calculate the number of lines of Here Document through the wc -l command on the command line:

$ wc -l << EOF 欢迎来到 php中文网 m.sbmmt.com EOF 3 # 输出结果为 3 行 $

We can also use Here Document is used in scripts, for example:

#!/bin/bash # author:php中文网 # url:m.sbmmt.com cat << EOF 欢迎来到 php中文网 m.sbmmt.com EOF

Execute the above script, the output result is:

欢迎来到 php中文网 m.sbmmt.com

/dev/null File

If you want to execute a certain command, But you don’t want to display the output results on the screen, then you can redirect the output to /dev/null:

$ command > /dev/null

/dev/null is a special file, and the content written to it will be discarded; If you try to read from this file, nothing will be read. However, the /dev/null file is very useful. Redirecting the output of the command to it will have the effect of "disabling output".

If you want to block stdout and stderr, you can write like this:

$ command > /dev/null 2>&1

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