14

The examples and explanations in this page are leaving me confused:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

Is there any practical difference between using 2<&1 and 2>&1? The second form (2>&1) is familiar to me, from working with the Unix shell.

The page linked above has:

To find File.txt, and then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to the Search.txt, type:
findfile file.txt>search.txt 2<&1

and also

To redirect all of the output, including handle 2 (that is, STDERR), from the ipconfig command to handle 1 (that is, STDOUT), and then redirect the ouput to Output.log, type:
ipconfig.exe>>output.log 2>&1

In the end, is there any difference in the results?

theglauber
  • 28,367
  • 7
  • 29
  • 47

1 Answers1

13

Some examples should show what happens:

c:\YourDir> cd FolderNotHere > nul
The system cannot find the path specified. 

You get the error stream

c:\YourDir>cd FolderNotHere > nul  2>&1

You get nothing, the error stream goes to the std output stream which goes to null.

c:\YourDir>cd > nul

You get nothing, the output stream goes to null.

c:\YourDir>cd > nul 1>&2
c:\YourDir

You get the std outout which has been sent to the error stream so it doesn't get redirected.

c:\YourDir>cd > nul 1<&2

This seams to do the same as 1>&2

Andy Morris
  • 3,393
  • 1
  • 21
  • 20
  • So it does look like 2<&1 and 2>&1 do the same thing, as i suspected. – theglauber Jan 18 '12 at 16:47
  • 3
    Excuse me. In `cd > nul 1>&2`, the `> nul` part mean "Redirect STDOUT to NUL", but the following `1>&2` mean "Forget that! Now redirect STDOUT to STDERR!", so STDOUT appears in the screen? If so, this example is _the same_ as put no redirection at all! – Aacini Jan 19 '12 at 03:16
  • Yes, that particular example was funny. But to me, the weirdest part is that the "direction" (< or >) doesn't seem to matter - 2<&1 is the same as 2>&1. I don't understand why; yet testing seems to confirm. – theglauber Jan 19 '12 at 19:53
  • 2
    It seems that < (with no number) mean 0<, and > (with no number) mean 1> (the same as >>), but WITH number < or > is the same! – Aacini Jan 21 '12 at 04:31
  • 3
    It would be much more clear if you would just describe what >, <, & and the numbers mean. – Daniel B Oct 02 '13 at 07:47