Skip to main content

How to Send Errors to the Same Place as Output in bash and zsh with 2>&1

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A command writes its normal output and its errors to two separate streams, so > file saves only the output, and the errors still land on the screen. Adding 2>&1 sends the errors to the same place. This guide saves both in one file, with real output from zsh 5.9 and bash 3.2.

Syntax​

Syntax
<command> > <file> 2>&1
PartWhat it means
> <file>Sends standard output, stream 1, to the file
2>&1Sends standard error, stream 2, to wherever stream 1 goes at that moment

The bash manual explains why 2>&1 comes last: "The order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command ls 2>&1 > dirlist directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist."

Prerequisites​

  • zsh or bash. This guide uses zsh 5.9, the default shell on macOS, and checks every command in bash 3.2 too.

Step 1: See Where the Errors Go Without It​

The folder holds notes.txt; missing.txt doesn't exist. List both into result.txt:

Terminal
ls notes.txt missing.txt > result.txt
Output
ls: missing.txt: No such file or directory
Terminal
cat result.txt
Output
notes.txt

zsh running ls notes.txt missing.txt > result.txt, which prints the missing.txt error on the screen, and cat result.txt, which shows only notes.txt.

The error appeared on the screen, and the file holds only notes.txt. cat prints a file's contents.

Step 2: Add 2>&1​

Terminal
ls notes.txt missing.txt > result.txt 2>&1

This time nothing appeared. The file holds both lines:

Terminal
cat result.txt
Output
ls: missing.txt: No such file or directory
notes.txt

zsh running ls notes.txt missing.txt > result.txt 2>&1, which prints nothing, and cat result.txt, which shows the error and notes.txt.

bash saved the same two lines.

Common Mistakes​

MistakeWhat happensDo this instead
Putting 2>&1 first: ls notes.txt missing.txt 2>&1 > result.txtThe error still showed on the screen, and the file held only notes.txtPut 2>&1 after > file

Troubleshooting​

SymptomLikely causeFix
Errors are missing from a log fileOnly standard output was redirected, or 2>&1 came before > fileAdd 2>&1 after > file

FAQ​

Is there a shorter way?​

Yes: &> result.txt saved both lines too, in zsh and in bash. The bash manual calls it "semantically equivalent to > word 2>&1".

What do the numbers 1 and 2 mean?​

They're file descriptors, the numbers of a program's streams. The bash manual: "standard output (file descriptor 1) and standard error (file descriptor 2)".

Conclusion​

ls notes.txt missing.txt > result.txt 2>&1 wrote the error and the listing into result.txt and nothing to the screen. Put 2>&1 after > file, or use &> file.

Additional Resources​