Wednesday, January 6, 2016

Windows: Redirect both stdout and stderr to a file

To redirect stdout to a file:
c:\>command_to_run > output.txt

To redirect both stdout and stderr to a file:
c:\>command_to_run > output.txt 2>&1

So what is 2>&1
Stack Overflow user Ayman Hourieh explained:

File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1.

Ref:
windows - Redirect stdout and stderr to a single file - Stack Overflow
bash - In the shell, what does " 2>&1 " mean? - Stack Overflow

No comments: