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 by default happens to be your terminal. S imilarly, a command typically writes its output to standard output, which by default is also your terminal.

The list of redirect commands is as follows:

Command Description
command > file Redirect the output to file.
command < file Redirect the input to file.
command >> file Redirect the output to file as an append.
n > file Redirect a file with a file descriptor of n to file.
n >> file Redirect a file with a file descriptor of n to file as an append.
n >& m Merge the output files m and n.
n <& m Merge the input files m and n.
<< tag Enter the content between the start tag tag and the end tag tag.

It is important to note that the file descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).


Output redirection

Redirection is typically 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 deposits the output into file1.

Note that any content that already exists in file1 will be replaced by new content. I f you want to add something new at the end of the file, use the operator.

Instance

Execute the who command below, which redirects the full output of the command in the user file (users):

$ who > users

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

You can use the cat command to view the contents of the file:

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

The output redirect overrides the contents of the file, see the following example:

$ echo "W3Cschool教程:www.w3cschool.cn" > users
$ cat users
W3Cschool教程:www.w3cschool.cn
$

If you don't want the contents of the file to be overwritten, you can use the append to the end of the file, for example:

$ echo "W3Cschool教程:www.w3cschool.cn" >> users
$ cat users
W3Cschool教程:www.w3cschool.cn
W3Cschool教程:www.w3cschool.cn
$

Enter the redirect

Like output redirection, the Unix command can also get input from a file, in the following syntax:

command1 < file1

This way, commands that would otherwise need to get input from the keyboard are transferred to the file read.

Note: The output redirect is greater than the sign, and the input redirect is less than the sign.

Instance

Then, in 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 two examples above are different: the first example outputs the file name;

command1 < infile > outfile

Replace the inputs and outputs at the same time, perform command1, read from the file infile, and then write the output to the outfile.

Redirect in-depth

In general, each Unix/Linux command opens three files at runtime:

  • Standard input file (stdin): The file descriptor for stdin is 0, and the Unix program reads data from stdin by default.
  • Standard output file (stdout): The file descriptor for stdout is 1, and the Unix program outputs data to stdout by default.
  • Standard Error File (stderr): The file descriptor for stderr is 2, and the Unix program writes an error message to the stderr stream.

By default, command and file redirect stdout to file, and command and file redirect stdin to file.

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

$ command 2 > file

If you want stderr to append to the end of the file file file, you can write this:

$ command 2 >> file

2 represents the standard error file (stderr).

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

$ command > file 2>&1

或者

$ command >> file 2>&1

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

$ command < file1 >file2

The command command redirects stdin to file1 and stdout to file2.


Here Document

Here Document is a special way to redirect input to an interactive shell script or program.

Its basic form is as follows:

command << delimiter
    document
delimiter

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

Attention:

  • The delimiter at the end must be written in the top grid, there must be no characters in front, and no characters at the end, including spaces and tab indentations.
  • Spaces before and after the start of the delimiter are ignored.

Instance

The number of lines in Here Document is calculated from the wc -l command on the command line:

$ wc -l << EOF
    欢迎来到
    W3Cschool教程
    www.w3cschool.cn
EOF
3          # 输出结果为 3 行
$

We can also use Here Document in scripts, such as:

#!/bin/bash
# author:W3Cschool教程
# url:www.w3cschool.cn

cat << EOF
欢迎来到
W3Cschool教程
www.w3cschool.cn
EOF

Execute the above script and output the results:

欢迎来到
W3Cschool教程
www.w3cschool.cn

/dev/null file

If you want to execute a command but don't want the output to appear on the screen, you can redirect the output to /dev/null:

$ command > /dev/null

/dev/null is a special file in which the contents written to it are discarded; However, the /dev/null file is useful for redirecting the output of a command to it, which has the effect of "prohibiting output".

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

$ command > /dev/null 2>&1

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