如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
当前回答
要批量地开始新一行,你所要做的就是添加“echo[”,像这样:
echo Hi!
echo[
echo Hello!
其他回答
有一个标准的功能echo:在cmd/bat-files中写入空行,这模拟了cmd-output中的新行:
@echo off
echo line1
echo:
echo line2
or
@echo line1 & echo: & echo line2
上面引用的cmd-file输出:
line1
line2
Use:
echo hello
echo:
echo world
经过一个不眠之夜,在阅读了这里所有的答案后,在阅读了大量的SS64 > CMD和大量的尝试和错误后,我发现:
(几乎)终极解决方案
博士TL;
... 对于早期采用者。
Important! |
---|
Use a text editor for C&P that supports Unicode, e.g. Notepad++! |
设置换行环境变量…
... 在当前CMD会话中
Important! |
---|
Do not edit anything between '= ' and '^ '! (There's a character in between though you don't see it. Neither here nor in edit mode. C&P works here.) |
:: Sets newline variables in the current CMD session
set \n=^&echo:
set nl=^&echo:
... 当前用户
Important! |
---|
Do not edit anything between (the second) '␣ ' and '^ '! (There's a character in between though you don't see it. Neither here nor in edit mode. C&P works here.) |
:: Sets newline variables for the current user [HKEY_CURRENT_USER\Environment]
setx \n ^&echo:
setx nl ^&echo:
... 对于本地机器
Important! |
---|
Do not edit anything between (the second) '␣ ' and '^ '! (There's a character in between though you don't see it. Neither here nor in edit mode. C&P works here.) |
:: Sets newline variables for the local machine [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
setx \n ^&echo: /m
setx nl ^&echo: /m
为什么只是快?
它不适用于在同一打印行中没有配对(开引号和闭引号)的双引号,除非唯一未配对的双引号是文本的最后一个字符,例如:
works: ""echo %\n%...after "newline". Before "newline"...%\n%...after "newline" (paired in each printed line) works: echo %\n%...after newline. Before newline...%\n%...after newline" (the only unpaired double-quote is the last character) doesn't work: echo "%\n%...after newline. Before newline...%\n%...after newline" (double-quotes are not paired in the same printed line) Workaround for completely double-quoted texts (inspired by Windows batch: echo without new line): set BEGIN_QUOTE=echo ^| set /p !=""" ... %BEGIN_QUOTE% echo %\n%...after newline. Before newline...%\n%...after newline"
它适用于完全单引号的文本,如:
echo '%\n%...after newline. Before newline...%\n%...after newline'
附加值:转义字符
Note |
---|
There's a character after the '= ' but you don't see it here but in edit mode. C&P works here. |
:: Escape character - useful for color codes when 'echo'ing
:: See https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting
set ESC=
颜色参见https://imgur.com/a/EuNXEar和https://gist.github.com/gerib/f2562474e7ca0d3cda600366ee4b8a45。
第二个附加价值:轻松获取Unicode字符
通过关键字获取87,461个Unicode字符(AToW)的绝佳页面:https://www.amp-what.com/。
的原因
The version in Ken's answer works apparently (I didn't try it), but is somehow...well...you see: set NLM=^ set NL=^^^%NLM%%NLM%^%NLM%%NLM% The version derived from user2605194's and user287293's answer (without anything between '=' and '^'): set nl=^&echo: set \n=^&echo: works partly but fails with the variable at the beginning of the line to be echoed: > echo %\n%Hello%\n%World! echo & echo:Hello & echo:World! echo is ON. Hello World due to the blank argument to the first echo. All others are more or less invoking three echos explicitely. I like short one-liners.
背后的故事
为了防止set \n=^&echo:在这里的回答中显示空白(并打印其状态),我第一次记住Alt+255用户是在Novell广泛使用网络和使用437和850等代码页的时候。但是0d255/0xFF在现在的Unicode中是大写的大写字母Y。
然后我想起来Unicode中有比普通的0d32/0x20更多的空格,但它们都被认为是空格,并导致与›␣相同的行为。
But there are even more: the zero width spaces and joiners which are not considered as whitespaces. The problem with them is, that you cannot C&P them since with their zero width there's nothing to select. So, I copied one that is close to one of them, the hair space (U+200A) which is right before the zero width space (U+200B) into Notepad++, opened its Hex-Editor plugin, found its bit representation E2 80 8A and changed it to E2 80 8B. Success! I had a non-whitespace character that's not visible in my \n environment variable.
就像肯的答案一样,只是用了延迟扩展。
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.
使用延迟展开的优点是,根本不需要进行特殊的字符处理。 Line1%LF%Line2将失败,因为解析器在单个换行处停止解析。
更多解释见 SO:在Vista/DOS批处理(.bat)文件中,长命令拆分为多行 那么:Windows命令解释器(CMD.EXE)如何解析脚本?
编辑:避免回声。
这并没有回答问题,因为问题是关于单个echo可以输出多行。
但是除了其他答案谁建议使用回声。要创建新行,应注意echo。是最糟糕的,因为它非常慢,它可以完全失败,因为cmd.exe搜索一个名为ECHO的文件,并试图启动它。
如果只打印空行,可以使用其中之一
echo,
echo;
echo(
echo/
echo+
echo=
而是回声的使用。, echo\或echo:应该避免,因为它们会非常慢,这取决于脚本执行的位置,比如网络驱动器。
当回显某些内容以重定向到文件时,多个回显命令将不起作用。我认为“>>”重定向器是一个不错的选择:
echo hello > temp echo world >> temp