views:

299

answers:

2

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?

+2  A: 

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.

developmentalinsanity
+1  A: 
tommieb75