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

ぐ巨炮叔叔 提交于 2019-12-22 06:31:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42211476/what-does-21-in-a-windows-command-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!