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.
ls /workspace /no/such 2> errors.txtThe 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.
ls /workspace /no/such > all.txt 2>&1
cat all.txtBoth 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.
ls /workspace /no/such 2>&1 > wrong.txt
wc -l wrong.txtThe 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.
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.
ls /workspace /no/such &> both.txt
cat both.txtThe 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.txtThe 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.
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.
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.