Doing some maintenance on a script, I found this line:
ping -n 40 127.0.0.1 > NUL 2>&1
I know from this question that everything up to the NUL causes the script to sleep for 39 seconds. But I don't know what the rest of the command does.
What does the 2>&1
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.