从 Windows 批处理文件向控制台回显空白(空)行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20691060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:11:50  来源:igfitidea点击:

Echo a blank (empty) line to the console from a Windows batch file

windowsbatch-fileecho

提问by DavidRR

When outputting status messages to the console from a Windows batch file, I want to output blank linesto break up the output. How do I do this?

当从 Windows 批处理文件向控制台输出状态消息时,我想输出空行以中断输出。我该怎么做呢?

采纳答案by DavidRR

Note: Though my original answer attracted several upvotes, I decided that I could do much better. You can find my original (simplistic and misguided) answer in the edit history.

注意:虽然我最初的回答吸引了好几票,但我决定我可以做得更好。您可以在编辑历史记录中找到我的原始(简单化和误导性)答案。

If Microsoft had the intent of providing a means of outputting a blank line from cmd.exe, Microsoft surely would have documented such a simple operation. It is this omission that motivated me to ask this question.

如果微软有意提供一种从 输出空行的方法cmd.exe,微软肯定会记录这样一个简单的操作。正是这种疏忽促使我提出这个问题。

So, because a means for outputting a blank line from cmd.exeis notdocumented, arguably one should consider any suggestion for how to accomplish this to be a hack. That means that there is no known method for outputting a blank line from cmd.exethat is guaranteedto work (or work efficiently) in allsituations.

因此,由于没有记录从文件中输出空行的cmd.exe方法,因此可以说,应该将有关如何实现这一点的任何建议视为hack。这意味着没有已知的方法可以保证所有情况下都能正常工作(或有效工作)。cmd.exe

With that in mind, here is a discussion of methods that have been recommended for outputting a blank line from cmd.exe. All recommendations are based on variations of the echocommand.

考虑到这一点,这里讨论了推荐用于从cmd.exe. 所有建议均基于echo命令的变体。



echo.

While this will work in many if not most situations, it should be avoidedbecause it is slower than its alternatives and actually can fail (see here, here, and here). Specifically, cmd.exefirst searches for a file named echoand tries to start it. If a file named echohappens to exist in the current working directory, echo.will fail with:

虽然这在许多情况下(如果不是大多数情况)都有效,但应避免使用,因为它比其替代方案慢并且实际上可能会失败(请参阅此处此处此处)。具体来说,cmd.exe首先搜索一个名为的文件echo并尝试启动它。如果echo当前工作目录中恰好存在名为的文件,echo.则会失败:

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


echo:
echo\

At the end of this answer, the author argues that these commands can be slow, for instance if they are executed from a network drive location. A specific reason for the potential slowness is not given. But one can infer that it may have something to do with accessing the file system. (Perhaps because :and \have special meaning in a Windows file system path?)

这个答案的最后,作者认为这些命令可能很慢,例如,如果它们是从网络驱动器位置执行的。没有给出潜在缓慢的具体原因。但是可以推断它可能与访问文件系统有关。(也许是因为:\在 Windows 文件系统路径中具有特殊含义?)

However, some may consider these to be safe options since :and \cannot appear in a file name. For that or another reason, echo:is recommended by SS64.com here.

然而,有些人可能认为这是因为安全选项:,并\不能出现在一个文件名。出于这个或其他原因,echo:SS64.com在这里推荐。



echo(
echo+
echo,
echo/
echo;
echo=
echo[
echo]

This lengthy discussionincludes what I believe to be all of these. Several of these options are recommended in this SO answeras well. Within the cited discussion, this postends with what appears to be a recommendation for echo(and echo:.

这个冗长的讨论包括我认为的所有这些。在这个 SO 答案中也推荐了其中的几个选项。在引用的讨论中,这篇文章的结尾似乎是对echo(和的建议echo:

My question at the top of this page does not specify a version of Windows. My experimentation on Windows 10 indicates that allof these produce a blank line, regardless of whether files named echo, echo+, echo,, ..., echo]exist in the current working directory. (Note that my question predates the release of Windows 10. So I concede the possibility that older versions of Windows may behave differently.)

我在本页顶部的问题没有指定 Windows 的版本。我在 Windows 10 上的实验表明,所有这些都会产生一个空行,无论当前工作目录中是否存在名为echo, echo+, echo,, ... 的echo]文件。(请注意,我的问题早于 Windows 10 的发布。所以我承认旧版本的 Windows 可能表现不同。)

In this answer, @jebasserts that echo(alwaysworks. To me, @jeb's answer implies that other options are less reliable but does not provide any detail as to why that might be. Note that @jeb contributed much valuable content to other references I have cited in this answer.

这个答案中@jeb断言echo(总是有效。对我来说,@jeb 的回答暗示其他选项不太可靠,但没有提供任何关于为什么会这样的细节。请注意,@jeb 为我在此答案中引用的其他参考文献贡献了许多有价值的内容。



Conclusion:Do notuse echo.. Of the many other options I encountered in the sources I have cited, the support for these two appears most authoritative:

结论:千万不能使用echo.。在我引用的来源中遇到的许多其他选项中,对这两个选项的支持似乎最权威:

echo(
echo:

But I have not found any strong evidence that the use of either of these will always be trouble-free.

但我还没有找到任何强有力的证据表明使用其中任何一个总是没有问题的。



Example Usage:

示例用法:

@echo off
echo Here is the first line.
echo(
echo There is a blank line above this line.

Expected output:

预期输出:

Here is the first line.

There is a blank line above this line.

回答by Sunny

Any of the below three options works for you:

以下三个选项中的任何一个都适合您:

echo[

echo(

echo. 

For example:

例如:

@echo off
echo There will be a blank line below
echo[
echo Above line is blank
echo( 
echo The above line is also blank.
echo. 
echo The above line is also blank.

回答by jeb

There is often the tip to use 'echo.'

通常有使用“回声”的提示。

But that is slow, and it could fail with an error message, as cmd.exe will search first for a file named 'echo' (without extension) and only when the file doesn't exists it outputs an empty line.

但这很慢,并且可能会失败并显示错误消息,因为 cmd.exe 将首先搜索名为“echo”(不带扩展名)的文件,并且仅当该文件不存在时才输出空行。

You could use echo(. This is approximately 20 times faster, and it works always. The only drawback could be that it looks odd.

你可以使用echo(. 这大约快 20 倍,并且始终有效。唯一的缺点可能是它看起来很奇怪。

More about the different ECHO:/\variants is at DOS tips: ECHO. FAILS to give text or blank line.

有关不同ECHO:/\变体的更多信息,请参阅DOS 提示:ECHO。无法给出文本或空行