What does "2>&1" in a Windows Command Do? What does "2>&1" in a Windows Command Do? windows windows

What does "2>&1" in a Windows Command Do?


Decomposing the line

ping -n 40 127.0.0.1

Send 40 ping packets to local host. If there is not any problem the default behaviour is to wait 1 second between packets, so it generates a 39 second delay

>nul   or   1>nul

Redirects anything written to the standard output stream (stream number 1) to the nul device. Anything sent to this device is discarded. The effect is that all the normal output of the ping command is hidden.

2>&1

This redirects anything written to the standard error stream (stream number 2). As in the previous case, this is done to hide output (errors in this case), but instead of directly requesting to write to the nul device (we could have done 2>nul), this syntax requests to send data in the standard error stream to a copy of the handle used in the standard output stream.