$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Redirecting stderr: 2>, 2>&1 & Why Order Matters

4 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:07

Targeting one stream

A number placed in front of a redirection operator names the file descriptor it changes. 2> redirects standard error, and 1> means the same as a plain >. Using the number is useful when a command prints results and diagnostics, because you can send each to a different place.

BASH1 lines
ls /workspace /no/such 2> errors.txt

The listing of /workspace appears on the terminal, and the complaint about /no/such is written into errors.txt. This is the cleanest way to keep a long run readable: the normal output stays visible while the errors are collected for later.

Merging with 2>&1

Sometimes you want both streams in the same place, such as a log file or a pipe to a filter. The construct 2>&1 duplicates file descriptor 2 onto file descriptor 1. After it runs, slot 2 is a second handle on whatever slot 1 pointed to at that moment. It does not remember the name of the file.

BASH2 lines
ls /workspace /no/such > all.txt 2>&1
cat all.txt

Both the listing and the error appear in all.txt. The redirections were processed left to right: first slot 1 was pointed at the file, and then slot 2 was pointed at wherever slot 1 now points.

Why the order matters

Reverse the two redirections and the result changes. In 2>&1 > file, slot 2 is copied onto slot 1 while slot 1 still points at the terminal. Then slot 1 is moved to the file. Standard error stays on the terminal, and only standard output goes to the file. The copy captured the old destination, not the new one.

BASH2 lines
ls /workspace /no/such 2>&1 > wrong.txt
wc -l wrong.txt

The error message appears on the terminal, and wrong.txt contains only the listing. The count from wc reflects just those lines. Reading redirections as a sequence of assignments made from left to right is the habit that prevents this mistake.

Tip

When in doubt, write the file redirection first and the 2>&1 second. The reading is then: send standard output to the file, then send standard error wherever standard output now goes.

The bash shorthand

Bash offers &> as a shorthand for sending both streams to one file, and &>> for appending both. These are bash extensions and are not in the POSIX shell, but they read clearly and avoid the ordering trap. In scripts that must run under other shells, keep the explicit form.

BASH2 lines
ls /workspace /no/such &> both.txt
cat both.txt

The output file contains the listing and the error, the same as the explicit > file 2>&1 form.

Redirecting a group to collect its errors

A redirection placed after a compound command, such as a braces group or a loop, applies to every command inside it. This is the easiest way to collect the errors from a whole batch of operations into one file without touching each command.

In the example, the two directory listings are both inside the group, so any complaints from either of them go into the same file, while the successful listing remains on the terminal.

{ ls /workspace; ls /no/such1; ls /no/such2; } 2> batch-errors.txt
wc -l < batch-errors.txt

The count is 2, one line for each missing path. The listing of /workspace printed normally. This pattern pairs well with the logging habit from the previous section: gather the complaints, then decide what to do with them.

Discarding errors and output

The special file /dev/null accepts any amount of data and throws it away. Sending a stream there is a common way to silence it. A script that tests whether a command works, but does not want to show its messages, redirects both streams to the null device and reads only the exit status.

BASH1 lines
ls /no/such 2> /dev/null; echo "status=$?"

The error is hidden and the status is 2. Silencing errors this way hides real problems as well, so reserve it for checks where the status is the only thing that matters.

Standard error in a pipeline

A pipe connects only standard output of the left command to standard input of the right. Standard error of the left command is not part of the pipe, so it still reaches the terminal. To pass it along with the data, merge it first with 2>&1, placed before the pipe.

BASH1 lines
ls /workspace /no/such 2>&1 | grep -c "cannot"

The count is 1, because the error message travelled through the pipe with the listing and grep found it. The next lesson covers pipes in detail.

Redirections change the file descriptor table

Visualizer

Redirections are applied left to right, before the command runs. >file 2>&1 and 2>&1 >file look alike, but they send stderr to different places.

1 / 4
Before the command
0/dev/pts/0
1/dev/pts/0
2/dev/pts/0
$ ls /nope >out.txt 2>&1

Bash starts with fds 0, 1 and 2 all pointing at the terminal. Redirections change this table before ls runs, and ls inherits whatever the table says.

ls /nope >out.txt 2>&1; echo "status $?"; cat out.txt
Terminal lab · 3 steps · real Linux in your browser

Order and merge streams

Merge streams, see why order matters, and collect errors from a group.

01
Merge both streams into one file
Send the listing and the error into all.txt, using the file redirection first: ``bash ls /workspace /no/such > all.txt 2>&1 ``
Show hint for step 1
Put > all.txt before 2>&1 so that descriptor 2 copies the file destination.
02
See why the order matters
Reverse the order. Now the pipe receives the error, while standard output goes to wrong.txt. Send what the pipe receives into seen.txt: ``bash ls /no/such 2>&1 > wrong.txt | cat > seen.txt ``
Show hint for step 2
2>&1 runs first, so descriptor 2 copies the pipe, and only then does > move descriptor 1 to the file.
03
Collect errors from a group
Put two failing listings in a braces group and redirect its standard error to errs.txt: ``bash { ls /no/such1; ls /no/such2; } 2> errs.txt ``
Show hint for step 3
A redirection after the braces applies to every command inside them.
Check yourself Question 1 of 4
Concept Check Meaning of 2>&1 recall

What does the redirection 2>&1 do?

Recap & Key Takeaways

A number before a redirection operator names the descriptor, so 2> sends standard error to a file while standard output keeps its destination.
2>&1 copies descriptor 2 onto wherever descriptor 1 points at that point, so the order of redirections changes the result.
Read redirections from left to right, with the file redirection first and 2>&1 after it when you want both streams in one file.
The left side of a pipe carries only standard output, so merge standard error with 2>&1 before the pipe to filter it too.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue