Shell Input/Output Redirection
Most UNIX system commands accept input from your terminal and send the generated output back to your terminal. A command typically reads input from a place called standard input, which by default is your terminal. Similarly, a command typically writes its output to standard output, which by default is also your terminal.
The redirection commands are listed as follows:
Command | Description |
---|---|
command > file | Redirects output to file. |
command < file | Redirects input from file. |
command >> file | Redirects output to file by appending. |
n > file | Redirects the file descriptor n to file. |
n >> file | Redirects the file descriptor n to file by appending. |
n >& m | Merges output files n and m. |
n <& m | Merges input files n and m. |
<< tag | Uses the content between the start tag and end 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).
Output Redirection
Redirection is generally achieved by inserting specific symbols between commands. Specifically, the syntax of these symbols is as follows:
command1 > file1
The above command executes command1 and then stores the output in file1.
Note that any existing content in file1 will be replaced by the new content. To append the new content to the end of the file, use the >> operator.
Example
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 to the terminal, because the output has been redirected from the default standard output device (terminal) to the specified file.
You can view the file content using the cat command:
$ cat users
_mbsetupuser console Oct 31 17:35
tianqixin console Oct 31 17:35
tianqixin ttys000 Dec 1 11:33
Output redirection overwrites the file content. See the following example:
$ echo "tutorialpro.org: www.tutorialpro.org" > users
$ cat users
tutorialpro.org: www.tutorialpro.org
$
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 "tutorialpro.org: www.tutorialpro.org" >> users
$ cat users
tutorialpro.org: www.tutorialpro.org
tutorialpro.org: www.tutorialpro.org
$
Input Redirection
Like output redirection, UNIX commands can also get input from a file. The syntax is:
command1 < file1
This way, commands that would normally require input from the keyboard will read the content from the file instead.
Note: Output redirection uses the greater-than symbol (>), and input redirection uses the less-than symbol (<).
Example
Continuing from the previous 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 the input from the users file:
$ wc -l < users
2
Note: The results of the above two examples are different: The first example outputs the filename; the second does not, because it only knows how to read from standard input.
command1 < infile > outfile
Simultaneously replaces input and output, executes command1, reads content from the infile, and writes the output to the outfile.
In-depth Redirection
Typically, each Unix/Linux command opens three files when running:
- Standard input file (stdin): The file descriptor for stdin is 0, and Unix programs default to reading data from stdin.
- Standard output file (stdout): The file descriptor for stdout is 1, and Unix programs default to outputting data to stdout.
- Standard error file (stderr): The file descriptor for stderr is 2, and Unix programs write error messages to the stderr stream.
By default, command > file redirects stdout to file, and command < file redirects stdin to file.
If you want to redirect stderr to file, you can write:
$ command 2>file
If you want to append stderr to the end of the file, you can write:
$ command 2>>file
2 represents the standard error file (stderr).
If you want to merge stdout and stderr and redirect them to a file, you can write it like this:
$ command > file 2>&1
or
$ command >> file 2>&1
If you want to redirect both stdin and stdout, you can write it like this:
$ command < file1 > file2
The 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
It passes the content between the two delimiters (document) as input to the command.
>
Note:
- The ending delimiter must be written flush left, with no characters before or after it, including spaces and tab indents.
- Spaces before and after the starting delimiter are ignored.
Example
To count the number of lines in a Here Document using the wc -l
command:
$ wc -l << EOF
欢迎来到
tutorialpro.org
www.tutorialpro.org
EOF
3 # The output result is 3 lines
$
We can also use Here Document in a script, for example:
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
cat << EOF
欢迎来到
tutorialpro.org
www.tutorialpro.org
EOF
Executing the above script will output:
欢迎来到
tutorialpro.org
www.tutorialpro.org
/dev/null File
If you want to execute a command but do not want the output to be displayed on the screen, you can redirect the output to /dev/null:
$ command > /dev/null
/dev/null is a special file where any content written to it is discarded; if you try to read from this file, nothing can be read. However, /dev/null is very useful; redirecting the output of a command to it achieves the effect of "suppressing output."
If you want to suppress both stdout and stderr, you can write it like this:
$ command > /dev/null 2>&1
>
Note: 0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).
There should be no space between 2 and >, 2> is a single entity that indicates error output.