5

I have a bat script which at one point redirects the stderr of a process to the stdout, and then writes it to a file. I used to do it like this:

process.exe 2>&1 > file.txt 

However, this doesn't redirect the stderr to the file ( for reasons I can't understand ). When I modified the line to :

process.exe > file.txt 2>&1 

The whole thing worked. Aren't these two equivalent?

3 Answers 3

8

The first example essentially does:

stderr = stdout; stdout = "file.txt"; 

So, stderr is still pointing at the original stdout. Your second example does:

stdout = "file.txt"; stderr = stdout; 

So, both stderr and stdout now reference file.txt. It's annoyingly subtle.

Sign up to request clarification or add additional context in comments.

Comments

2

The redirection 2>&1 works at the end of the command line. It will not work as the first redirection parameter, the redirection requires a filename and the 2>&1 at the end. You're effectively trying to redirect stderr but there is no placeholder to store the stderr messages hence it failed. The shortcut to remembering this is

 executable > some_file 2>&1 

Hope this helps, Best regards, Tom.

Comments

1

By the way, for reasons I do not completely understand, a think like

process.exe > result.txt 2<&1 

also seems to work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.