Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash redirection

Reference for Bash redirection: send output to files, read input from files, redirect stderr, merge streams with 2>&1, open descriptors, noclobber and /dev/null.

Every process starts with three open channels called file descriptors: 0 is standard input, 1 is standard output and 2 is standard error. Redirection changes where those channels point before the command runs, so output can go to a file, input can come from a file, and errors can be separated from normal output.

The shell sets up redirections from left to right, which explains why 2>&1 is so sensitive to its position on the line. The program itself never sees the operators.

Terminal
cmd > out.txt          # stdout to a file (overwrite)
cmd >> out.txt         # append
cmd < in.txt           # stdin from a file
cmd > all.txt 2>&1     # stdout and stderr to the same file

Try it

Bash Files, append and separate streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Order matters and descriptors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
[n]> file     [n]>> file     [n]< file
[n]>&m        [n]<&m         [n]>&-        [n]<&-
&> file       &>> file       [n]<> file     >| file
Operator Description
> file Send standard output to file, creating or truncating it.
>> file Append instead of truncating.
< file Read standard input from file.
n> file The same for descriptor n; 2> file redirects errors.
n>&m Make descriptor n a copy of m. 2>&1 points stderr to wherever stdout points now.
n>&- Close descriptor n.
&> file, &>> file Bash shortcut for > file 2>&1 (and its append form).
<> file Open file for reading and writing.
>| file Overwrite even when noclobber is set.
<<, <<< Here-document and here-string; see Here-documents.

Returns: the command's own status; a failed redirection (missing file, no permission) prints an error and gives status 1 without running the command.

Errors / edge cases: a redirection is performed even if the command is not found, so nosuchcmd > f still creates an empty file f.

Usage notes

  • Left to right. cmd > f 2>&1 sends both to the file; cmd 2>&1 > f sends errors to the old stdout (usually the terminal) and only stdout to the file.
  • Never read and write the same file in one command (sort f > f). The shell truncates f before sort starts. Write to a temporary file, then mv; or use sponge or sort -o f f.
  • /dev/null swallows anything written and returns end-of-file on read. cmd >/dev/null 2>&1 is the classic silence idiom.
  • Pipes carry only stdout. To pipe stderr as well, use cmd 2>&1 | other or cmd |& other in Bash.
  • Redirections apply to compound commands too: { a; b; } > out, while ...; done < file, ( ... ) 2>err.
  • Permanent changes use exec: exec > log 2>&1. See exec.
  • Descriptor numbers above 9 are possible via exec {fd}> file, where Bash picks a free number and stores it in fd.
  • Special files: Bash emulates /dev/stdin, /dev/stdout, /dev/stderr, /dev/tcp/host/port and /dev/udp/host/port even if the system lacks them.
  • Spaces: 2> file is stderr, but 2 > file passes 2 as an argument. No space between the number and the operator.
  • Append races: >> is atomic for small writes, which is why logs written with >> from several processes mostly stay line-intact.