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?
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.
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.
By the way, for reasons I do not completely understand, a think like
process.exe > result.txt 2<&1
also seems to work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With