windows 如何在批处理文件中回显换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/132799/
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
How can I echo a newline in a batch file?
提问by Brian R. Bondy
How can you you insert a newline from your batch file output?
如何从批处理文件输出中插入换行符?
I want to do something like:
我想做类似的事情:
echo hello\nworld
Which would output:
这将输出:
hello
world
采纳答案by Grimtron
echo hello & echo.world
echo hello & echo.world
This means you could define & echo.
as a constant for a newline \n
.
这意味着您可以将& echo.
换行符定义为常量\n
。
回答by Matt Lacey
Use:
用:
echo hello
echo.
echo world
回答by Grimtron
Here you go, create a .bat file with the following in it :
在这里,创建一个 .bat 文件,其中包含以下内容:
@echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
echo There should be a newline%NL%inserted here.
echo.
pause
You should see output like the following:
您应该会看到如下输出:
There should be a newline
inserted here.
Press any key to continue . . .
You only need the code between the REM statements, obviously.
显然,您只需要 REM 语句之间的代码。
回答by macropas
There is a standard feature echo:
in cmd/bat-files to write blank line, which emulates a new line in your cmd-output:
echo:
在 cmd/bat-files 中有一个标准特性来写空行,它在你的 cmd 输出中模拟一个新行:
@echo off
@echo line1
@echo:
@echo line2
Output of cited above cmd-file:
上面引用的 cmd 文件的输出:
line1
line2
回答by jeb
Like the answer of Ken, but with the use of the delayed expansion.
像 Ken 的答案,但使用了延迟扩展。
setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)
echo Line1!\n!Line2
echo Works also with quotes "!\n!line2"
First a single linefeed character is created and assigned to the \n-variable.
This works as the caret at the line end tries to escape the next character, but if this is a Linefeed it is ignored and the next character is read and escaped (even if this is also a linefeed).
Then you need a third linefeed to end the current instruction, else the third line would be appended to the LF-variable.
Even batch files have line endings with CR/LF only the LF are important, as the CR's are removed in this phase of the parser.
首先创建单个换行符并将其分配给 \n 变量。
这在行尾的插入符号尝试转义下一个字符时起作用,但如果这是换行符,则将其忽略并读取并转义下一个字符(即使这也是换行符)。
然后您需要第三个换行符来结束当前指令,否则第三行将附加到 LF 变量。
即使批处理文件以 CR/LF 结尾,只有 LF 很重要,因为在解析器的这个阶段删除了 CR。
The advantage of using the delayed expansion is, that there is no special character handling at all.echo Line1%LF%Line2
would fail, as the parser stops parsing at single linefeeds.
使用延迟扩展的优点是,根本没有特殊的字符处理。echo Line1%LF%Line2
会失败,因为解析器在单个换行符处停止解析。
More explanations are at
SO:Long commands split over multiple lines in Vista/DOS batch (.bat) file
SO:How does the Windows Command Interpreter (CMD.EXE) parse scripts?
更多解释在
SO:在 Vista/DOS 批处理 (.bat) 文件中拆分成多行的长命令
SO:Windows 命令解释器 (CMD.EXE) 如何解析脚本?
Edit: Avoid echo.
编辑:避免 echo.
This doesn't answer the question, as the question was about single echo
that can output multiple lines.
这不能回答问题,因为问题是关于echo
可以输出多行的single 。
But despite the other answers who suggests the use of echo.
to create a new line, it should be noted that echo.
is the worst, as it's very slow and it can completly fail, as cmd.exe searches for a file named ECHO
and try to start it.
但是,尽管其他答案建议使用echo.
来创建新行,但应该注意这echo.
是最糟糕的,因为它非常慢并且可能完全失败,因为 cmd.exe 搜索名为的文件ECHO
并尝试启动它。
For printing just an empty line, you could use one of
要仅打印一个空行,您可以使用其中之一
echo,
echo;
echo(
echo/
echo+
echo=
But the use of echo.
, echo\
or echo:
should be avoided, as they can be really slow, depending of the location where the script will be executed, like a network drive.
但是使用的echo.
,echo\
或echo:
应该避免,因为他们可以很慢,这取决于该脚本将被执行,就像一个网络驱动器的位置。
回答by coltonon
echo.
Enough said.
echo.
说够了。
If you need it in a single line, use the &
. For example,
如果在一行中需要它,请使用&
. 例如,
echo Line 1 & echo. & echo line 3
would output as:
将输出为:
Line 1
line 3
Now, say you want something a bit fancier, ...
现在,假设你想要一些更高级的东西,......
set n=^&echo.
echo hello %n% world
Outputs
输出
hello
world
Then just throw in a %n%
whenever you want a new line in an echo statement. This is more close to your \n
used in various languages.
然后,%n%
只要您想在 echo 语句中添加新行,就可以输入 a 。这更接近你\n
在各种语言中的使用。
Breakdown
分解
set n=
sets the variable n
equal to:
set n=
设置变量n
等于:
^
Nulls out the next symbol to follow:
^
清空要跟随的下一个符号:
&
Means to do another command on the same line. We don't care about errorlevel(its an echo statement for crying out loud), so no &&
is needed.
&
意味着在同一行上执行另一个命令。我们不关心错误级别(它是大声喊叫的回声声明),所以&&
不需要。
echo.
Continues the echo statement.
echo.
继续 echo 语句。
All of this works because you can actually create variables that are code, and use them inside of other commands. It is sort of like a ghetto function, since batch is not exactly the most advanced of shell scripting languages. This only works because batch's poor usage of variables, not designating between ints, chars, floats, strings, etc naturally.
所有这些都有效,因为您实际上可以创建代码变量,并在其他命令中使用它们。它有点像 ghetto 函数,因为批处理并不是最先进的 shell 脚本语言。这只是因为批处理对变量的不良使用,而不是自然地在整数、字符、浮点数、字符串等之间指定。
If you are crafty, you could get this to work with other things. For example, using it to echo a tab
如果你很狡猾,你可以让它与其他东西一起工作。例如,使用它来回显选项卡
set t=^&echo. ::there are spaces up to the double colon
回答by Crend King
When echoing something to redirect to a file, multiple echo commands will not work. I think maybe the ">>" redirector is a good choice:
当回显某些内容以重定向到文件时,多个回显命令将不起作用。我认为“>>”重定向器可能是一个不错的选择:
echo hello > temp echo world >> temp
回答by Crend King
Just like Grimtron suggests - here is a quick example to define it:
就像 Grimtron 建议的那样 - 这是一个定义它的快速示例:
@echo off
set newline=^& echo.
echo hello %newline%world
Output
输出
C:\>test.bat
hello
world
回答by test30
If you need to put results to a file, you can use
如果您需要将结果放入文件,您可以使用
(echo a & echo. & echo b) > file_containing_multiple_lines.txt
回答by albert
You can also do like this,
你也可以这样做,
(for %i in (a b "c d") do @echo %~i)
The output will be,
输出将是,
a
b
c d
Note that when this is put in a batch file, '%' shall be doubled.
请注意,将其放入批处理文件时,'%' 应加倍。
(for %%i in (a b "c d") do @echo %%~i)