Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Shell Input/Output Redirection

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 is 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 from 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 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 (STDERR).


Output redirection

Redirection is generally 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 stores the output in file1.

Note that any existing content in file1 will be replaced by the new content. To append new content to the end of the file, use the >> operator.

Example

Run the following who command, which redirects the command's full output to the users 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 use the cat command to view the file's content:



$ 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's content; see the following example:



$ echo "Runoob Tutorial:www.runoob.com" > users

$ cat users

Runoob Tutorial:www.runoob.com

$

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



$ echo "Runoob Tutorial:www.runoob.com" >> users

$ cat users

Runoob Tutorial:www.runoob.com

Runoob Tutorial:www.runoob.com

$


Input redirection

Like output redirection, Unix commands can also get input from a file; the syntax is:



command1 < file1

In this way, a command that would normally get input from the keyboard instead reads content from a file.

Note: output redirection is the greater-than sign (>), input redirection is the less-than sign (<).

Example

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



$ wc -l users

       2 users

You can also redirect input from the users file:



$  wc -l < users

       2 

Note: The two examples above produce different results: the first one outputs the filename, while 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.

Redirections in Depth

In general, every Unix/Linux command opens three files when it runs:

By default, command > file redirects stdout to file, 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 the file, you can write it like this:



$ command 2>>file

2 represents the standard error file (stderr).

If you want to combine stdout and stderr and redirect them to file, you can write it like this:



$ command > file 2>&1



或者



$ 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 kind of redirection in Shell, used to redirect input to an interactive Shell script or program.

Its basic form is as follows:



command << delimiter

    document

delimiter

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

Note:

  • The closing delimiter must be written flush left, with no characters before it and no characters after it, including spaces and tab indentation.
  • Spaces before and after the opening delimiter are ignored.
  • Example

    Count the number of lines in a Here Document on the command line with the wc -l command:

    
    
    $ wc -l << EOF
    
        欢迎来到
    
        Runoob Tutorial
    
        www.runoob.com
    
    EOF
    
    3          # 输出结果为 3 行
    
    $
    
    

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

    
    
    #!/bin/bash
    
    # author:Runoob Tutorial
    
    # url:www.runoob.com
    
    
    
    cat << EOF
    
    欢迎来到
    
    Runoob Tutorial
    
    www.runoob.com
    
    EOF
    
    

    Run the above script, and the output is:

    
    
    欢迎来到
    
    Runoob Tutorial
    
    www.runoob.com
    
    

    The /dev/null File

    If you want to run a command but do not want to display its 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, and if you try to read from it, you get 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 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).

    Here, 2 and > There must be no spaces between them,2> only when they are joined together does it represent error output.