Shell input / output redirection

Most UNIX systems to accept commands from your terminal input and output generated is sent back to your terminal. A command typically read from a local input called standard input, by default, this happens to be your terminal. Similarly, a command usually writes its output to standard output, by default, this is your terminal.

Redirect command list is as follows:

command Explanation
command> file Redirect the output to a file.
command <file Redirect input to the file.
command >> file The output to be redirected to the appending file.
n> file The file descriptor for the n files redirected to file.
n >> file The file descriptor n to file additional ways to redirect the file.
n> & m The output file m and n are merged.
n <& m The input file m and n are merged.
<< Tag The start tag and end tag tag tag content between as input.

Note that the file descriptor 0 is usually standard input (STDIN), 1 is the standard output (STDOUT), 2 is the standard error output (STDERR).


Output redirection

Redirect typically by between command to insert specific symbols to achieve. In particular, these symbols shown in the following syntax:

command1 > file1

The above command will execute command1 and then output the content stored in file1.

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

Examples

Run the following command who complete it will redirect the output of a command in the user files (users):

$ who > users

After the execution, there is no output at the terminal, it 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 contents 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 overwrites the contents of the file, see the following example:

$ echo "本教程:www.w3resource.net" > users
$ cat users
本教程:www.w3resource.net
$

If you do not want the contents of the file is overwritten, you can use the >> append to a file, for example:

$ echo "本教程:www.w3resource.net" >> users
$ cat users
本教程:www.w3resource.net
本教程:www.w3resource.net
$

Input redirection

And as output redirection, Unix command can take input from a file, the syntax is:

command1 < file1

Thus, the original need to get input from the keyboard will be transferred to the command file to read the content.

Note: The output redirection greater than symbol (>), input redirection is less than (<).

Examples

Then the above example, we need to count the number of rows users file, execute the following command:

$ wc -l users
       2 users

You can also redirect input files to users:

$  wc -l < users
       2 

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

command1 < infile > outfile

Replace both input and output, execute command1, infile read from the file, and then write the output to the outfile.

Redirect depth explanation

Under normal circumstances, each Unix / Linux command runs will open three files:

  • Standard input file (stdin): stdin file descriptor to 0, Unix default program reads data from stdin.
  • Standard output file (stdout): stdout to file descriptor 1, Unix program defaults to stdout output data.
  • Standard error (stderr): stderr file descriptor to 2, Unix program writes error messages to stderr stream.

By default, command> file redirect stdout to a file, command <file will stdin redirected to file.

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

$ command 2 > file

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

$ command 2 >> file

2 represents the standard error file (stderr).

If you want to merge after the stdout and stderr redirected to the file, you can write:

$ command > file 2>&1

或者

$ command >> file 2>&1

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

$ command < file1 >file2

command command stdin redirected to file1, redirect stdout to file2.


Here Document

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

Its basic form is as follows:

command << delimiter
    document
delimiter

It is the role of content (document) is transmitted to the two delimiter between command as input.

note:

  • Ending delimiter must be the top grid write, can not have any of the characters in front of the back can not have any characters, including spaces and tab indentation.
  • Spaces before and after the start delimiter is ignored.

Examples

The number of rows in the command line computing Here Document by the wc -l command:

$ wc -l << EOF
    欢迎来到
    本教程
    www.w3resource.net
EOF
3          # 输出结果为 3 行
$

We can also Here Document used in a script, for example:

#!/bin/bash
# author:本教程
# url:www.w3resource.net

cat << EOF
欢迎来到
本教程
www.w3resource.net
EOF

Implementation of the above script, output:

欢迎来到
本教程
www.w3resource.net

/ 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, its contents are written to be discarded; if you try to read from the file, then what can not read. However, / dev / null file is very useful to redirect the output of the command to it, it will act as a "prohibit output" effect.

If you want to shield stdout and stderr, you can write:

$ 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).