English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Shell Input/Output Redirection

Most UNIX system commands accept input from your terminal and send the output back to your terminal. A command typically reads input from a place called standard input, by default, this is exactly your terminal. Similarly, a command usually writes its output to standard output, by default, this is also your terminal.

List of redirection commands as follows:

Command Description
command > file Redirect output to file.
command < file Redirect input to file.
command >> file Redirect the output in append mode to file.
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 the output files m and n.
n <& m Merge the input files m and n.
<< tag Take the content between the start tag tag and the end tag tag as input.

It should be noted that file descriptor 0 is usually standard input (STDIN),1 Is the standard output (STDOUT),2 Is the 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 command1Then store the output content in file1.

Note anyfile1The existing content within will be replaced by the new content. If you want to add new content to the end of the file, please use the >> operator.

Online Examples

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

$ who > users

After execution, no information is output to the terminal. This is 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 content of the file:

$ 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 "基础教程网:www.oldtoolbag.com" > users
$ cat users
Basic Tutorial Website: www.oldtoolbag.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 "基础教程网:www.oldtoolbag.com" >> users
$ cat users
Basic Tutorial Website: www.oldtoolbag.com
Basic Tutorial Website: www.oldtoolbag.com
$

Input Redirection

Like output redirection, Unix commands can also obtain input from a file, with the syntax as follows:

command1 < file1

As a result, the command that was originally intended to read input from the keyboard is transferred to reading content from a file.

Attention: Output redirection is represented by the greater than sign (>), and input redirection is represented by the less than sign (<).

Online Examples

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

$ wc -l users
       2 users

Input can also be redirected 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 will not, because it only knows to read content from standard input.

command1 < infile > outfile

Replace input and output simultaneously and execute command1Reads content from the file infile and then writes the output to outfile.

In-depth explanation of redirection

Generally, each Unix/When running Linux commands, they will always open three files:

  • Standard input file (stdin): stdin's file descriptor is 0, Unix programs read data from stdin by default.

  • Standard output file (stdout): The file descriptor of stdout is1Unix programs output data to stdout by default.

  • Standard error file (stderr): The file descriptor of stderr is2Unix 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 to redirect stderr to file, you can write it like this:

$ command 2>file

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

$ command 2>>file

2 It indicates the standard error file (stderr).

If you want to redirect stdout and stderr to 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 file1Redirect 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 the command.

Note:

  • The delimiter at the end must be flush with the left margin, without any characters before it, and without any characters after it, including spaces and tab indentation.

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

Online Examples

Through wc in the command line -l command calculates the number of lines of Here Document:

$ wc -l << EOF
    Welcome to
    Basic Tutorial Website
    www.oldtoolbag.com
EOF
3          # Output result is 3 line
$

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

#!/bin/bash
# author:Basic Tutorial Website
# url:www.oldtoolbag.com
cat << EOF
Welcome to
Basic Tutorial Website
www.oldtoolbag.com
EOF

Execute the above script, the output is:

Welcome to
Basic Tutorial Website
www.oldtoolbag.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, any content written to it will be discarded; if you try to read content from the file, nothing will be read. But /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 suppress stdout and stderr, you can write it like this:

$ command > /dev/null 2>&1

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

Here 2 and > There should be no spaces between them,2> Error output is indicated only when integrated.