windows cmd: why does the order in which you specify output redirection matter?

cmd, io-redirection, windows

Solution

The redirections are simply parsed left to right. Initially `stdout` and `stderr` are pointing to the console, so when you redirect `stderr` to where `stdout` is currently pointing in the second example, you are directing `stderr` to the console. You then subsequently redirect `stdout` to a file.

The explanations and examples here are quite good for further reference.

Problem

if you do ``` ipconfig.exe 1> output.log 2>&1 ``` this will direct all output(both stdout and stderr) to "output.log". However, if you do (changing the order in which you specify desired redirections) ``` ipconfig.exe 2>&1 1> output.log ``` this will not achieve the intended effect of printing both output streams to "output.log" because "stderr" in this case will be printed to the console. I suspect this has to do with the way "cmd" parses commands that gives different meanings depending on the order in which you specify redirects. If so, what are the semantic rules and where are they documented? I reckon this is something worth finding out as it could waste people hours by making them scratching their head trying to figure out why their redirection is not working.

Original source