command > file 2>&1 puts both the output and the errors in the file. command 2>&1 > file puts only the output there and still prints the errors on the screen. Same symbols, swapped order, different result, and the second form appears in scripts more often than anyone would like. The rule behind it takes one sentence once it clicks. Every example below was run in Bash 5.2 against a two-line test script that prints one line to standard output and one to standard error, and the results shown are exactly what the shell produced.
Three streams with numbers
The test script
#!/bin/bash
echo "out: normal message"
echo "err: something failed" >&2Every program starts with three open file descriptors: 0 for standard input, 1 for standard output, 2 for standard error. echo writes to 1 by default; >&2 sends the second line to 2 instead. On a terminal both land on the screen, so they look identical. Redirection is where they come apart.
Only stdout goes through >
$ ./both.sh > f.txt
err: something failed
[file: out: normal message]
> is shorthand for 1>. It moved standard output into the file and left standard error pointing at the terminal, which is why the error still appeared. This is by design: errors should reach a person even when the output is being saved.
Throwing errors away
$ ./both.sh 2>/dev/null
out: normal message
2>/dev/null points descriptor 2 at the null device, which discards whatever is written to it. It is useful for silencing a known, harmless warning, and dangerous as a habit, because it silences the unknown, harmful ones too.

What 2>&1 actually does
Copy, not link
2>&1 means: make descriptor 2 point wherever descriptor 1 points right now. The POSIX shell specification describes [n]>&word as duplicating one output file descriptor from another. It copies the current destination. It does not create a permanent link between the two.
Why the order matters
The Bash manual states it outright: the order of redirections is significant. Its own example contrasts ls > dirlist 2>&1, which sends both outputs to the file, with ls 2>&1 > dirlist, which sends only standard output there, because standard error was duplicated from standard output before standard output was redirected.
The test showed both:
$ ./both.sh > f.txt 2>&1
[file: out: normal message | err: something failed]
$ ./both.sh 2>&1 > f.txt
err: something failed
[file: out: normal message]
Redirections are processed left to right. In the first, > f.txt points 1 at the file, then 2>&1 copies that, so 2 also points at the file. In the second, 2>&1 copies where 1 points at that moment, the terminal, and only then does > f.txt move 1. Descriptor 2 keeps the old destination.
The shortcut
$ ./both.sh &> f.txt
[file: out: normal message | err: something failed]
The Bash manual lists &>word and >&word as ways to send both streams to one file, says &>word is semantically equivalent to >word 2>&1, and states that of the two forms the first is preferred. It is shorter and impossible to get in the wrong order.
Pipes see only stdout
The default pipe
$ ./both.sh | tr a-z A-Z
err: something failed
OUT: NORMAL MESSAGE
A pipe connects one program’s standard output to the next program’s standard input. Standard error bypassed tr entirely and printed unchanged, lowercase, straight to the terminal.
Sending errors into the pipe too
$ ./both.sh 2>&1 | tr a-z A-Z
OUT: NORMAL MESSAGE
ERR: SOMETHING FAILED
$ ./both.sh |& tr a-z A-Z
OUT: NORMAL MESSAGE
ERR: SOMETHING FAILED
Here 2>&1 comes before the pipe in the text, but the pipe was set up first, so descriptor 1 already pointed into it and the copy followed. The Bash manual describes |& as shorthand for 2>&1 |, connecting the command’s standard error, in addition to its standard output, to the next command.
Only errors through the pipe
$ ./both.sh 2>&1 >/dev/null | tr a-z A-Z
ERR: SOMETHING FAILED
This is the “wrong order” form put to good use. 2>&1 copies the pipe destination into 2; then >/dev/null discards normal output. Only errors reach tr. It is the standard trick for searching or counting a program’s error messages while ignoring everything else.

Swapping and saving streams
Swapping stdout and stderr
$ ./both.sh 3>&1 1>&2 2>&3 | tr a-z A-Z
out: normal message
ERR: SOMETHING FAILED
The extra descriptor 3 acts as a temporary holder: 3 copies the pipe, 1 copies the terminal (from 2), and 2 copies the pipe (from 3). Normal output went to the terminal and errors went through the pipe, the reverse of the default.
Redirecting a whole script with exec
Inside a script, exec without a command applies a redirection to the rest of the script:
exec 2>err.log
echo "to stdout"
ls /nonexistent
echo "still running"The two echo lines printed normally. The ls error went into err.log and nothing else did. This is a tidy way to make a cron job or startup script keep its own error log.
Writing to a named descriptor
exec 3>notes.txt
echo first >&3
echo second >&3
exec 3>&-notes.txt ended up with “first” and “second.” Descriptor 3 stayed open across both writes, so the file wasn’t reopened and truncated each time, and 3>&- closed it at the end.
The mistakes that lose data
Two redirections to the same file
$ ./both.sh > f.txt 2> f.txt
[file: err: something failed]
The normal message vanished. Each redirection opened the file separately, both starting at the beginning, so the second write landed on top of the first. 2>&1 avoids this because both descriptors then share one open file and one position.
Appending both streams
$ ./both.sh >> f.txt 2>&1; ./both.sh >> f.txt 2>&1
[file: out | err | out | err]
>> appends instead of truncating, and 2>&1 after it shares the appending descriptor. Two runs produced four lines, in order. This is the right form for log files written by repeated jobs.
noclobber
$ set -o noclobber; echo a > f.txt; echo b > f.txt
bash: line 1: f.txt: cannot overwrite existing file
$ echo c >| f.txt
With noclobber on, the Bash manual says > fails if the target exists and is a regular file. >| overrides it. Turning noclobber on in an interactive shell stops a mistyped > from wiping a file that took hours to produce.
&> in a script that runs under sh
The same &> line run by dash, the shell behind /bin/sh on Debian and Ubuntu, did something completely different:
$ dash -c './both.sh &> g2.txt'
out: normal message
err: something failed
file bytes: 0
Both lines printed to the screen and the file was created empty. POSIX shells read & as “run this in the background” and > g2.txt as a separate, empty redirection. A script that starts with #!/bin/sh and uses &> doesn’t fail; it quietly runs the command in the background and truncates the file. The portable spelling is > file 2>&1.

Quick reference
The forms worth memorizing
- Output to file, errors to screen:
cmd > file - Both to one file:
cmd > file 2>&1, or in Bash,cmd &> file - Both appended to a log:
cmd >> file 2>&1 - Discard errors:
cmd 2>/dev/null - Discard everything:
cmd > /dev/null 2>&1 - Both into a pipe:
cmd 2>&1 | next, or in Bash,cmd |& next - Only errors into a pipe:
cmd 2>&1 >/dev/null | next - See it and save it:
cmd 2>&1 | tee file
When the lines come out in the wrong order
Sending both streams to one file keeps every line, but not always in the order they were written. A small Python script that alternated three normal lines with three error lines was run with > file 2>&1. The file read:
err 0
err 1
err 2
out 0
out 1
out 2
All the errors came first. When standard output goes to a file or pipe rather than a terminal, many programs collect it in a buffer and write it in chunks, while standard error is written immediately. The same script run with Python’s -u option, which turns off output buffering, produced the lines in their true alternating order. For tools without such an option, stdbuf -oL from GNU coreutils can request line buffering for programs that use the standard C library. When a log’s timeline matters, buffering is worth checking before blaming the program.
The one-sentence rule
Read redirections left to right, and treat 2>&1 as “send errors wherever output is going at this point in the line.” Every example above follows from that, including the ones that look like typos and turn out to be the only way to get the job done.
Sources
- bash(1), Linux manual page (REDIRECTION and Pipelines): https://man7.org/linux/man-pages/man1/bash.1.html
- Shell Command Language (Redirection, 2.7), The Open Group Base Specifications (POSIX.1-2024): https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html
