477 Contributors: 30
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook


Download bash eBook


  • command </path/to/file # Redirect standard input to file
  • command >/path/to/file # Redirect standard output to flie
  • command file_descriptor>/path/to/file # Redirect output of file_descriptor to file
  • command >&file_descriptor # Redirect output to file_descriptor
  • command file_descriptor>&another_file_descriptor # Redirect file_descriptor to another_file_descriptor
  • command <&file_descriptor # Redirect file_descriptor to standard input
  • command &>/path/to/file # Redirect standard output and standard error to file


internal file descriptorAn integer.
directionOne of >, < or <>
external file descriptor or path& followed by an integer for file descriptor or a path.


UNIX console programs have an input file and two output files (input and output streams, as well as devices, are treated as files by the OS.) These are typically the keyboard and screen, respectively, but any or all of them can be redirected to come from — or go to — a file or other program.

STDIN is standard input, and is how the program receives interactive input. STDIN is usually assigned file descriptor 0.

STDOUT is standard output. Whatever is emitted on STDOUT is considered the "result" of the program. STDOUT is usually assigned file descriptor 1.

STDERR is where error messages are displayed. Typically, when running a program from the console, STDERR is output on the screen and is indistinguishable from STDOUT. STDERR is usually assigned file descriptor 2.

The order of redirection is important

command > file 2>&1

Redirects both (STDOUT and STDERR) to the file.

command 2>&1 > file

Redirects only STDOUT, because the file descriptor 2 is redirected to the file pointed to by file descriptor 1 (which is not the file file yet when the statement is evaluated).

Each command in a pipeline has its own STDERR (and STDOUT) because each is a new process. This can create surprising results if you expect a redirect to affect the entire pipeline. For example this command (wrapped for legibility):

$ python -c 'import sys;print >> sys.stderr, "Python error!"' \
| cut -f1 2>> error.log

will print "Python error!" to the console rather than the log file. Instead, attach the error to the command you want to capture:

$ python -c 'import sys;print >> sys.stderr, "Python error!"' 2>> error.log \
| cut -f1 

Related Examples