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. Similarly, a command usually writes its output to standard output, which by default is also your terminal.
The list of redirection commands is as follows:
| Command | Description |
|---|---|
| command > file | Redirect output to file. |
| command < file | Redirect input to file. |
| command >> file | Redirect output to file in append mode. |
| n > file | Redirect the file with file descriptor n to file. |
| n >> file | Redirect the file with file descriptor n to file in append mode. |
| n >& m | Merge output files m and n. |
| n <& m | Merge input files m and n. |
| << tag | Use the content between the start marker tag and the end marker 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 implemented 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 into file1.
Note that any existing content in file1 will be replaced by the new content. If you want to append the new content to the end of the file, use the >> operator.
Examples
Execute the following who command, which redirects the complete output of the command to the user file (users):
$ who > users
After execution, no information is output on the terminal, because the output has been redirected from the default standard output device (the terminal) to the specified file.
You can use the cat command to view the file contents:
$ 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 contents. See the example below:
$ echo "Example:www.example.com" > users $ cat users Example:www.example.com $
If you don't want the file content to be overwritten, you can use >> to append to the end of the file, for example:
$ echo "Example:www.example.com" >> users $ cat users Example:www.example.com Example:www.example.com $
Input Redirection
Just like output redirection, Unix commands can also get input from a file, with the syntax:
command1 < file1
In this way, commands that originally needed to get input from the keyboard will instead read content from the file.
Note: Output redirection uses the greater-than sign (>), and input redirection uses the less-than sign (<).
Examples
Continuing with the above example, we need to count the number of lines in the users file. Execute the following command:
$ wc -l users
2 users
You can also redirect input to the users file:
$ wc -l < users
2
Note: The results of the above two examples are different: the first example outputs the file name; the second does not, because it only knows to read content from standard input.
command1 < infile > outfile
To replace both input and output at the same time, execute command1, read content from the file infile, and then write the output to outfile.
In-depth Explanation of Redirection
In general, every Unix/Linux command opens three files when running:
- Standard input file (stdin): stdin's file descriptor is 0, and Unix programs read data from stdin by default.
- Standard output file (stdout): stdout's file descriptor is 1, and Unix programs output data to stdout by default.
- Standard error file (stderr): stderr's file descriptor is 2, and Unix programs 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:
$ command 2>file
If you want stderr to be appended to the end of the file, you can write:
$ 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:
$ command > file 2>&1 或者 $ command >> file 2>&1
If you want to redirect both stdin and stdout, you can write:
$ 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.
Note:
- The ending delimiter must be written at the beginning of the line, with no characters before it and no characters after it, including spaces and tab indentation.
- Spaces before and after the starting delimiter are ignored.
Examples
On the command line, use thewc -lcommand to count the number of lines in the Here Document:
$ wc -l << EOF
欢迎来到
Example
www.example.com
EOF
3 # 输出结果为 3 行
$
We can also use Here Document in scripts, for example:
#!/bin/bash # author:Example # url:www.example.com cat << EOF 欢迎来到 Example www.example.com EOF
Execute the above script, output result:
欢迎来到 Example www.example.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; anything written to it is discarded. If you try to read content from this file, you will read nothing. However, the /dev/null file is very useful. Redirecting a command's output to it has the effect of "suppressing output".
If you want to suppress both stdout and stderr, you can write:
$ command > /dev/null 2>&1
Other ExtensionsNote:0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).
Here, the2and>there must be no spaces between them,2>only when they are combined does it represent error output.