China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-23 10:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » What does 2>nul 1>nul in batch processing statements mean? DigestI View 29,426 Replies 57
Floor 16 Posted 2006-05-11 14:00 ·  中国 广东 广州 天河区 电信
中级用户
★★
Credits 466
Posts 237
Joined 2005-10-12 20:53
20-year member
UID 43413
Status Offline
The first time I saw NUL used in this way
Floor 17 Posted 2006-05-11 16:18 ·  中国 福建 泉州 电信
高级用户
★★
论坛灌水专业户
Credits 613
Posts 266
Joined 2006-04-19 22:47
20-year member
UID 54113
From 河南省
Status Offline
Hehe, after looking at it for a long time, I just don't understand!~~
饮马恒河畔,剑指天山西,碎叶城揽月,库叶岛赏雪,黑海之滨垂钓,贝尔加湖面张弓;中南半岛访古,东京废墟祭祖!
Floor 18 Posted 2006-05-12 13:17 ·  中国 甘肃 兰州 中移铁通
初级用户
Credits 90
Posts 38
Joined 2006-01-06 18:41
20-year member
UID 48522
Status Offline
Originally posted by chenall at 2006-3-26 14:38:
You can't use handle 3 simultaneously if it's specified twice. Try changing the second one to echo. 1>nul 4>nul and see.
Indeed, it works.
To restore, use echo 2>con 5>con respectively, echo 1>con 6>con. If you only want to...

Among the objects we are discussing, there are two >. Using i and j to represent numbers from 0 to 9, if we regard the handle as a pointer to the device, then by default:
Handle Points to
stdin con
stdout con
stderr con

When the first > in the command cnd i>nul j>nul is executed, the system temporarily stores the target of in an unused handle space, such as , for restoration when the command ends. However, the second > modifies the temporarily stored content, thus leaving an impact on subsequent commands.
In fact, cnd i>nul j>nul will also temporarily store the original content of in another unused space. So
The change in output from cnd 2>nul 3>nul can be restored using cnd 2>&3 4>&3.
Since the temporary storage location is selected by the system and cannot be controlled by the user, the values cannot be determined.
(Each time I test, to ensure the certainty of the handle space, I always re-enter cmd.)
Floor 19 Posted 2006-05-13 18:49 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Regarding:

Because the temporary location is selected by the system and users cannot control it, so the value cannot be determined


I guessed in floor 13 that cmd stores the handle before modification in "unvisited undefined handles (3-9)". Here, I will clarify again. The "unvisited" here means that the user has never explicitly specified it in the current cmd command line, not that cmd itself has not used it. And there may be multiple "unvisited" handles, and cmd will choose the first one. If you have counterexamples, feel free to put them forward.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 20 Posted 2006-05-14 17:15 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re regarding:

I have my own counter-evidence. After executing the following two sentences, the command line is still blocked.

echo test 3>nul
echo test 1>nul 3>nul

That is to say, although we have accessed handle 3 in the first sentence, we can still use 3>nul in the second sentence to change the backup handle stdout[1].

Therefore, the handle backup strategy of cmd may be like this: first use the undefined handle 3, if it is detected that this handle is in the backup state and is changed, then switch to the next handle.

In addition, there are some other discoveries:

1. cd 1>nul 2>nul 3>nul 4>nul can directly change the output of stdout and stderr;
2. cd 0<nul 3<nul or cd 0<nul 3>nul will cause cmd to continuously echo the command line prompt;
3. cd 0<nul 3<file will directly cause the cmd window to close;
4. After cd 1>nul 3>nul, the behavior of cls will also fail, so it is confirmed that cls is also an output behavior;

Finally, regarding the copying of handles, it can actually be understood as the redirection of handles, but this time the target of redirection is no longer a device or a file, but another handle.

To give an example, the device or file is a reservoir, the handle is its pipe, the command is the water pump or filter on the pipe, and cmd is a self-contained water cycle system. The pipes of the reservoir are numbered from 0-9, there are 10 in total. The 0# pipe is the inlet pipe, the 1# pipe is the drain pipe, the 2# pipe is the fault test pipe, and the 3-9# pipes are unused backup pipes. And 0< is actually opening the valve of the inlet pipe, 1> is opening the valve of the drain pipe, and 1>&2 is opening the connection valve from the 1# pipe to the 2# pipe. And >nul is using a water pump to empty the water.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 21 Posted 2006-05-15 11:38 ·  中国 甘肃 兰州 中移铁通
初级用户
Credits 90
Posts 38
Joined 2006-01-06 18:41
20-year member
UID 48522
Status Offline
Re Willsort:
Through trial and error, one can guess the system's handle usage strategy. For example, in the sentence "
cnd 2>nul 3>nul changes the output, and it can be restored with cnd 2>&3 4>&3", the number 4 is the result of the guess, which is based on the relatively clear situation of handle usage just when entering cmd.
If handle redirection is repeatedly used in a batch script, it is difficult to judge the situation of the handle space. So I said "the system decides, and users can hardly control".
I agree with your concept of "unused handles", and the test has confirmed this concept. In fact, I feel that clear concepts are crucial; otherwise, it will cause great harm. Just like the ambiguous explanations in Windows help, which will only make people more confused.
Floor 22 Posted 2006-10-12 08:00 ·  中国 浙江 衢州 电信
银牌会员
★★★
Credits 1,270
Posts 548
Joined 2004-05-31 00:00
22-year member
UID 25754
Gender Male
Status Offline
Bump
Floor 23 Posted 2006-10-12 09:47 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Classic, and it broadens the thinking! Handle redirection!
Floor 24 Posted 2006-10-12 10:15 ·  中国 浙江 温州 电信
初级用户
Credits 30
Posts 15
Joined 2005-08-13 01:44
20-year member
UID 41606
Gender Male
Status Offline
Can't understand~ Working hard...
Floor 25 Posted 2006-10-12 11:41 ·  中国 重庆 璧山区 电信
中级用户
★★
Credits 235
Posts 109
Joined 2006-08-24 00:52
19-year member
UID 61161
Gender Male
Status Offline
I like it, but still can't understand it
Floor 26 Posted 2006-10-12 13:34 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

The two are really admirable, but in the end, it still gives the feeling that the principle of handle redirection is not clearly explained, although several examples are given.
Floor 27 Posted 2006-10-12 21:55 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Just know that such a thing exists. Those who don't understand can refer to "Introduction to Linux Shell"

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 28 Posted 2006-10-14 03:41 ·  泰国 曼谷 True
新手上路
Credits 8
Posts 2
Joined 2006-04-24 12:01
20-year member
UID 54368
Gender Male
Status Offline
After reading this article, I have gained a lot. However, I still hope that experts can come to guide everyone. Give it a thumbs up.
Floor 29 Posted 2007-03-17 20:29 ·  中国 上海 黄浦区 电信
初级用户
Credits 49
Posts 22
Joined 2005-08-04 13:44
20-year member
UID 41332
Gender Male
Status Offline
Profound, still trying to understand it
Floor 30 Posted 2007-03-18 01:43 ·  中国 广东 广州 电信
中级用户
★★
脚本爱好者
Credits 238
Posts 93
Joined 2007-03-11 13:38
19-year member
UID 81417
Gender Male
From GZ
Status Offline
Wow! Where did you learn these profound knowledge? It's extremely deep!
Forum Jump: