windows 是否可以在批处理文件的回显行中放置换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1721258/
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 06:54:17  来源:igfitidea点击:

Is it possible to put a new line character in an echo line in a batch file?

windowsbatch-file

提问by Benj

Is it possible to put a new line character in an echo line in a batch file?

是否可以在批处理文件的回显行中放置换行符?

Basically I want to be able to do the equivalent of:

基本上我希望能够做相当于:

echo Hello\nWorld

You can do this easily enough in Linux, but I can't work out how to do it in Windows.

在 Linux 中你可以很容易地做到这一点,但我不知道如何在 Windows 中做到这一点。

回答by Blind Trevor

echo.prints an empty line.

echo.打印一个空行。

Example:

例子:

echo Hello
echo.
echo world

prints

印刷

Hello

world

回答by jeb

It can be solved with a single echo.

它可以通过单个回声解决。

You need a newline character \nfor this. There are multiple ways to get a new line into the echo

\n为此,您需要一个换行符。有多种方法可以让新行进入echo

1) This sample use the multiline caret to add a newline into the command,
the empty line is required

1) 本示例使用多行插入符在命令中添加换行符,
需要空行

echo Hello^

world

2) The next solution creates first a variable which contains one single line feed character.

2) 下一个解决方案首先创建一个包含一个换行符的变量。

set \n=^


rem ** Two empty lines are required

Or create the new line with a slightly modified version

或者使用稍微修改的版本创建新行

(set \n=^
%=DONT REMOVE THIS=%
)

And use this character with delayed expansion

并使用此字符延迟扩展

setlocal EnableDelayedExpansion
echo Hello!\n!world

To use a line feed character with the percent expansionyou need to create a more complex sequence

要使用带有百分比扩展的换行符,您需要创建一个更复杂的序列

echo Hello^%\n%%\n%world

Or you can use the New line hack

或者你可以使用New line hack

REM Creating a Newline variable (the two blank lines are required!)
set \n=^


set NL=^^^%\n%%\n%^%\n%%\n%
REM Example Usage:
echo There should be a newline%NL%inserted here.

But only the delayed expansion of the newline works reliable, also inside of quotes.

但只有换行符的延迟扩展才可靠,也在引号内。

回答by Clifford

After a little experimentation I discovered that it is possible to do it without issuing two separate echo commands as described in How can you echo a newline in batch files?. However to make it work you will need a text editor that does not translate CR to CR+LF.

经过一些实验后,我发现可以在不发出两个单独的 echo 命令的情况下执行此操作,如如何在批处理文件中回显换行符中所述?. 但是,要使其工作,您将需要一个不会将 CR 转换为 CR+LF 的文本编辑器。

Type:

类型:

@echo First Line

then with NumLock on, hold down the ALT key and type 10 on the numeric keypad before releasing ALT (you mustuse the numeric keypad, not the top-row number keys). This will insert a CR character. Then type the second line. Depending on your editor and how it handles CR compared with CR+LF you may get:

然后在 NumLock 打开的情况下,按住 ALT 键并在释放 ALT 之前在数字小键盘上键入 10(您必须使用数字小键盘,而不是顶行数字键)。这将插入一个 CR 字符。然后输入第二行。根据您的编辑器以及它如何处理 CR 与 CR+LF 相比,您可能会得到:

@echo First Line?Second Line

or

或者

@echo First Line
Second Line

This works from the command line and will work in a batch file so long as the text editor does not translate CR to CR+LF (which Windows/DOS editors do unless you configure them not to). If the CR is converted to CR+LF, or if you use just LF, the second line is interpreted as a new command.

只要文本编辑器不将 CR 转换为 CR+LF(Windows/DOS 编辑器会这样做,除非您将它们配置为不这样做),这可以从命令行运行,并且可以在批处理文件中运行。如果 CR 转换为 CR+LF,或者如果您只使用 LF,则第二行将被解释为新命令。

However, I cannot see why this would be preferable over simply:

但是,我不明白为什么这比简单地更可取:

@echo First Line
@echo Second Line

回答by Benj

Ahaha,

啊哈哈,

I think I've worked out something close enough...

我想我已经找到了足够接近的东西......

echo hello&echo.&echo world

Produces:

产生:

hello

你好

world

世界

回答by Reu Roo

echo.or echo(

回声。回声(

will do the blank new line. Hope this is helpful.

将做空白的新行。希望这是有帮助的。

回答by Wiretap

I found this very informative, so wanted to post a better example using the answers provided

我发现这非常有用,因此想使用提供的答案发布更好的示例

This provides a nicely formatted usage message

这提供了格式良好的使用消息

if "%1" == """" goto usage

:usage
 echo  USAGE: %0 [Set properties using -D flag] [Ant Task to Run]  &
 echo.                                                             &
 echo        Availble Command line properties                      &
 echo        --------------------------------                      &  
...

回答by martin

I think it is not possible. You could only try an ascii-character to this: http://www.asciitable.com/But this will perhaps crash your batch-file.

我认为这是不可能的。您只能尝试使用 ascii 字符:http: //www.asciitable.com/但这可能会使您的批处理文件崩溃。