17.12. I/O Redirection

Every process begins with three open file streams: standard in (stdin), standard out (stdout), and standard error (stderr). In addition to providing an file interface to access stored files, Unix provides a file interface to all of the abstractions it implements, including devices like the terminal or the keyboard. The stdin file stream is where the process’s input is read from, and stdout and stderr are where the process' output and error output are written. By default stdin reads from the keyboard and stdout and stderr write to the terminal. However, you can change where a program’s stdin, stdout, and stderr originate from or redirect to using I/O redirection.

Each of stdin, stdout, and stderr has a file descriptor associated with it. A file descriptor is a positive integer value that refers to an open file. Unix implements a file interface to many things that are not regular files and directories. One example is a file interface to devices like a keyboard or a terminal. When a new process starts up, it starts with three open "files" corresponding to stdin, stdout, and stderr with file descriptors 0, 1, and 2, respectively. These file descriptor values specify which of stdin, stdout, or stderr, are being redirected when executed at the command line:

  • < redirect stdin

  • > or 1> redirect stdout

  • 2> redirect stderr.

These operators can be combined in a command to redirect one or more of stdin, stdout, and stderr to a file, overwriting its contents. The shell also provides shorthand notation to combine stdout and stderr redirection to a single file, and to append output to an existing file:

  • &> redirect stdin and stdout to same file. Note that &>[outfile] is equivalent to 1> [outfile] 2>&1, which redirects stdout to outfile and redirects stderr to stdout.

  • >> append stdout to a file.

Here are a few examples:

  • Redirect stdout to other files using > or 1> (we use wc to check that the files are the same sizes, and diff to see that they are identical after the call to cat):

    $ wc quote
      4  26 160 quote
    
    $ cat quote
    The function of education is to teach one
    to think intensively and to think critically.
    Intelligence plus character - that is the
    goal of true education.
    
    $ cat quote > another        # >: redirect cat's stdout to file "another"
    
    $ wc another
      4  26 160 another
    
    $ cat quote 1> yetanother    # 1>: redirect cat's stdout to a file (same as >)
    
    $ diff -s another yetanother # check to see that both files are identical
  • Redirect stdin from a file using <:

    $ wc < quote                # <: redirect wc's stdin from "quote" file
      4  26 160 quote
  • Redirect stderr to a file using 2>:

    $ cat blah
    cat: blah: No such file or directory
    
    $ cat blah 2> error_out     # 2>: redirect stderr to a file "error_out"
    $ wc error_out
     1  7 37 error_out
  • Append program’s stdout to a file using >>:

    $ echo "Again:" >> another  # append echo's output to file "another"
    $  wc another
      5  27 167 another
    
    $ sort quote >> another     # append sort's output to file "another"
    
    $ wc another
      9  53 327 another
    
    $ cat another
    The function of education is to teach one
    to think intensively and to think critically.
    Intelligence plus character - that is the
    goal of true education.
    Again:
    Intelligence plus character - that is the
    The function of education is to teach one
    goal of true education.
    to think intensively and to think critically.
  • Redirect both stdout and stderr to the same file using &> (in this example the file blah doesn’t exist, so cat will write an error message to stderr, and the file quote does exist, so cat will output its contents to stdout):

    $ cat blah quote &> err_out
    
    $ cat err_out
    cat: blah: No such file or directory
    The function of education is to teach one
    to think intensively and to think critically.
    Intelligence plus character - that is the
    goal of true education.
  • Redirect all three from different files (in this example, there is no stderr output, so the grep_error file should be empty after this call):

    grep teach < another 1> grep_out 2> grep_error
    
    $ cat grep_out
    The function of education is to teach one
    The function of education is to teach one
    
    $ cat grep_err
    $

17.12.1. References

For more information: