从Windows批处理文件向控制台回显空白(空)行 [英] Echo a blank (empty) line to the console from a Windows batch file

查看:136
本文介绍了从Windows批处理文件向控制台回显空白(空)行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从Windows批处理文件向控制台输出状态消息时,我想输出空白行以分解输出.我该怎么办?

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

推荐答案

注意:尽管我最初的回答引起了几次反对,但我还是决定可以做得更好.您可以在编辑历史记录中找到我的原始答案(简单而误导的).

如果Microsoft打算提供一种从cmd.exe输出空白行的方法,则Microsoft肯定会记录这种简单的操作.正是这种疏忽促使我提出这个问题.

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.

因此,由于没有记录从>输出空白行的方法,因此可以说,应该将任何实现此建议的建议视为 hack .这意味着没有已知的方法可以从保证在 all 情况下正常工作(或有效地工作)的cmd.exe输出空白行.

So, because a means for outputting a blank line from cmd.exe is not documented, 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.exe that is guaranteed to work (or work efficiently) in all situations.

考虑到这一点,这里讨论了建议用于从cmd.exe输出空白行的方法.所有建议均基于echo命令的变体.

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 echo command.

echo.

虽然这在很多情况下(即使不是大多数情况)都可以使用,但 应避免使用 ,因为它比其他方法要慢,并且实际上可能会失败(请参见此处此处).具体来说,cmd.exe首先搜索名为echo的文件,然后尝试启动它.如果当前工作目录中存在名为echo的文件,则echo.将失败,并显示以下信息:

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

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


echo:
echo\

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

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?)

但是,由于:\不能出现在文件名中,因此某些人可能认为这些是安全的选项.由于上述原因或其他原因,SS64.com在此处建议使用echo:.

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(
echo+
echo,
echo/
echo;
echo=
echo[
echo]

这个冗长的讨论包括了我认为所有这些内容.还建议在此SO答案中选择其中几个选项.在引用的讨论中,此帖子结尾似乎是echo(echo:的建议.

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

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

My question at the top of this page does not specify a version of Windows. My experimentation on Windows 10 indicates that all of 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.)

此答案中, @jeb 断言echo( 总是有效.对我而言,@ jeb的答案暗示其他选项的可靠性较差,但未提供任何可能的原因的详细信息.请注意,@ jeb为我在此答案中引用的其他参考文献提供了很多有价值的内容.

In this answer, @jeb asserts that echo( always works. 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.

结论:请勿使用echo..在我引用的资源中遇到的许多其他选择中,对这两个的支持似乎是最权威的:

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

echo(
echo:

但是我还没有找到任何有力的证据表明使用这两种方法始终不会带来麻烦.

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

用法示例:

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

预期输出:

Here is the first line.

There is a blank line above this line.

这篇关于从Windows批处理文件向控制台回显空白(空)行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆