Batch - 重定向符号Redirection >

回到顶部(go to top)

 总结

Don't use a piping operator, which is what ">" Redirection is.

不要使用管道运算符,即“>”。

 

Display text 文本显示

To display a text on screen we have the ECHO command:

ECHO Hello world

This will show the following text on screen:

Hello world

When I say "on screen", I'm actually referring to the "DOS Prompt", "console" or "command window", or whatever other "alias" is used.

 

Streams 流

The output we see in this window may all look alike, but it can actually be the result of 3 different "streams" of text, 3 "processes" that each send their text to thee same window.

Those of you familiar with one of the Unix/Linux shells probably know what these streams are:

  • Standard Output
  • Standard Error
  • Console

 

Standard Output is the stream where all, well, standard output of commands is being sent to.
The ECHO command sends all its output to Standard Output.

ECHO 命令,都是将其输出到 Stand Output流中。

Standard Error is the stream where many (but not all) commands send their error messages.

And some, not many, commands send their output to the screen bypassing Standard Output and Standard Error, they use the Console. By definition Console isn't a stream.

There is another stream, Standard Input: many commands accept input at their Standard Input instead of directly from the keyboard.
Probably the most familiar example is MORE:

DIR /S | MORE

where the MORE command accepts DIR's Standard Output at its own Standard Input, chops the stream in blocks of 25 lines (or whatever screen size you may use) and sends it to its own Standard Output.

(Since MORE's Standard Input is used by DIRMORE must catch its keyboard presses (the "Any Key") directly from the keyboard buffer instead of from Standard Input.)

 

command >nul

^ This says to pipe the standard-output stream to null.

example:

1
ECHO Hello world>NUL

will show nothing on screen. That's because >NUL redirects all Standard Output to the NUL device, which does nothing but discard it.  

 

command 2>nul

^ This says to pipe the standard-error stream to null.

command 2>&1

^ This says to pipe the standard-error stream to the same place as the standard-output stream.

 

Redirection 重定向

1
2
uses 1 for Standard Output and 2 for Standard Error.<br><br>1>nul 意思是不显示命令运行的正确提示
2>nul 是不显示错误提示

如果放在一起就是, 正确提示&错误提示将都不显示

 >是重定向符号, nul是空设备的意思, 把提示输入到空设备就不显示了 

详细解释

You may be familiar with "redirection to NUL" to hide command output:

ECHO Hello world>NUL

will show nothing on screen.
That's because >NUL redirects all Standard Output to the NUL device, which does nothing but discard it.

Now try this (note the typo):

EHCO Hello world>NUL

The result may differ for different operating system versions, but in Windows XP I get the following error message:

'EHCO' is not recognized as an internal or external command, operable program or batch file.

This is a fine demonstration of only Standard Output being redirected to the NUL device, but Standard Error still being displayed.

Redirecting Standard Error in "true" MS-DOS (COMMAND.COM) isn't possible (actually it is, by using the CTTY command, but that would redirect all output including Console, and input, including keyboard).
In Windows NT 4 and later (CMD.EXE) and in OS/2 (also CMD.EXE) Standard Error can be redirected by using 2> instead of >

A short demonstration. Try this command:

ECHO Hello world 2>NUL

What you should get is:

Hello world

You see? The same result you got with ECHO Hello world without the redirection.
That's because we redirected the Standard Error stream to the NUL device, but the ECHO command sent its output to the Standard Output stream, which was not redirected.

Now make a typo again:

EHCO Hello world 2>NUL

What did you get? Nothing
That's because the error message was sent to the Standard Error stream, which was in turn redirected to the NUL device by 2>NUL

When we use > to redirect Standard Output, CMD.EXE interprets this as 1>, as can be seen by writing and running this one-line batch file "test.bat":

DIR > NUL

Now run test.bat in CMD.EXE and watch the result:

C:\>test.bat

C:\>DIR  1>NUL

C:\>_

It looks like CMD.EXE uses 1 for Standard Output and 2 for Standard Error. We'll see how we can use this later.

Ok, now that we get the idea of this concept of "streams", let's play with it.
Copy the following code into Notepad and save it as "test.bat":

@ECHO OFF
ECHO This text goes to Standard Output
ECHO This text goes to Standard Error 1>&2
ECHO This text goes to the Console>CON

Run test.bat in CMD.EXE, and this is what you'll get:

C:\>test.bat
This text goes to Standard Output
This text goes to Standard Error
This text goes to the Console

C:\>_

Now let's see if we can separate the streams again.
Run:

test.bat > NUL

and you should see:

C:\>test.bat
This text goes to Standard Error
This text goes to the Console

C:\>_

We redirected Standard Output to the NUL device, and what was left were Standard Error and Console.

Next, run:

test.bat 2> NUL

and you should see:

C:\>test.bat
This text goes to Standard Output
This text goes to the Console

C:\>_

We redirected Standard Error to the NUL device, and what was left were Standard Output and Console.

Nothing new so far. But the next one is new:

test.bat > NUL 2>&1

and you should see:

C:\>test.bat
This text goes to the Console

C:\>_

This time we redirected both Standard Output and Standard Error to the NUL device, and what was left was only Console.
It is said Console cannot be redirected, and I believe that's true. I can assure you I did try!

To get rid of screen output sent directly to the Console, either run the program in a separate window (using the START command), or clear the screen immediately afterwards (CLS).

In this case, we could also have used test.bat >NUL 2>NUL
This redirects Standard Output to the NUL device and Standard Error to the same NUL device.
With the NUL device that's no problem, but when redirecting to a file one of the redirections will lock the file for the other redirection.
What 2>&1 does, is merge Standard Error into the Standard Output stream, so Standard output and Standard Error will continue as a single stream.

  

 

posted on   frank_cui  阅读(422)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示