Windows 如何在批处理文件中回显换行符?

Windows 如何在批处理文件中回显换行符?,windows,batch-file,newline,Windows,Batch File,Newline,如何从批处理文件输出中插入换行符 我想做一些类似的事情: echo hello\n世界 这将产生: 你好 世界 使用: echo hello&echo.world 这意味着您可以将&echo.定义为换行符的常量\n在这里,创建一个.bat文件,其中包含以下内容: @echo off REM Creating a Newline variable (the two blank lines are required!) set NLM=^ set NL=^^^%NLM%%NLM%^%NLM%%

如何从批处理文件输出中插入换行符

我想做一些类似的事情:

echo hello\n世界
这将产生:

你好 世界 使用:


echo hello&echo.world


这意味着您可以将
&echo.
定义为换行符的常量
\n

在这里,创建一个.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
您应该看到如下输出:

There should be a newline
inserted here.

Press any key to continue . . .

显然,您只需要REM语句之间的代码

在回显要重定向到文件的内容时,多个回显命令将不起作用。我认为“>>”重定向器可能是一个不错的选择:

echo hello > temp echo world >> temp echo hello>temp 回声世界>>温度
正如Grimtron所建议的,这里有一个简单的例子来定义它:

@echo off
set newline=^& echo.
echo hello %newline%world
输出
这对我有效,无需延迟扩展:

@echo off
(
echo ^<html^> 
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause
@echo关闭
(
回声^
回音^
回声你好
回音^
回音^
)
暂停
它这样写输出:

<html>
<body>
Hello
</body>
</html>
Press any key to continue . . .

你好
按任意键继续。

在cmd/bat文件中有一个标准功能
echo:
用于写入空行,它模拟cmd输出中的新行:

@echo off
@echo line1
@echo:
@echo line2
上述cmd文件的输出:

line1

line2

就像Ken的答案一样,但是使用了延迟扩展

setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)

echo Line1!\n!Line2
echo Works also with quotes "!\n!line2"
首先创建一个换行符并将其分配给\n-变量。
这是因为行尾的插入符号尝试转义下一个字符,但如果这是换行符,则会忽略它,并读取和转义下一个字符(即使这也是换行符)。
然后需要第三行提要来结束当前指令,否则第三行将追加到LF变量。
即使批处理文件也有带有CR/LF的行结尾,只有LF才是重要的,因为CR在解析器的这个阶段被删除

使用延迟扩展的优点是,根本没有特殊字符处理。
echo Line1%LF%Line2
将失败,因为解析器将在单个换行时停止解析

更多解释见

编辑:避免回声。

这并不能回答问题,因为问题是关于可以输出多行的单个
echo

但是,尽管有其他答案建议使用
echo.
创建新行,但应该注意的是
echo.
是最糟糕的,因为它非常慢,而且可能完全失败,因为cmd.exe搜索名为
echo
的文件并尝试启动它

对于只打印空行,可以使用

echo,
echo;
echo(
echo/
echo+
echo=

但是应该避免使用
echo.
echo\
echo:
,因为它们可能非常慢,这取决于脚本执行的位置,例如网络驱动器。

您也可以这样做

(for %i in (a b "c d") do @echo %~i)
输出将是,

a
b
c d
请注意,将其放入批处理文件时,“%”应加倍

(for %%i in (a b "c d") do @echo %%~i)

如果有人来这里是因为他们想回显MINGW make文件中的空行,我使用

@cmd/c echo.

只需使用
echo。
会导致可怕的
进程\u begin:CreateProcess(NULL,echo.,…)失败。
错误消息


我希望这至少能帮助另外一个人:)

你可以使用
@echo
(@echo+[space]+[unsectable space])

注:不安全空间可通过Alt+0160获得

希望有帮助:)


[编辑]嗯,你说得对,我需要把它放在一个生成文件中,它在那里工作得很好。我想我的答案不适合批量文件。。。我的坏。

代码>回声。说得够多了

如果需要在单行中使用,请使用
&
。比如说,

echo Line 1 & echo. & echo line 3
将输出为:

Line 1

line 3
现在,假设你想要更华丽一点的东西,

set n=^&echo.
echo hello %n% world
输出

hello
world
然后,只要在echo语句中添加新行,就可以插入一个
%n%
。这更接近您在各种语言中使用的
\n

细分

set n=
将变量
n
设置为:

^
将后面的下一个符号置空:

&
表示在同一行上执行另一个命令。我们不关心errorlevel(它是大声呼喊的回声语句),所以不需要
&

echo。
继续echo语句

所有这些都是有效的,因为您实际上可以创建作为代码的变量,并在其他命令中使用它们。它有点像一个贫民区函数,因为批处理并不是最先进的shell脚本语言。这只是因为batch对变量的使用很差,不能在int、char、float、string等之间自然地进行指定

如果你很狡猾,你可以把它和其他东西一起使用。例如,使用它来回显选项卡

set t=^&echo.     ::there are spaces up to the double colon

如果需要将结果放入文件,可以使用

(echo a & echo. & echo b) > file_containing_multiple_lines.txt

Ken和Jeb解决方案运作良好

但是新行只使用LF字符生成,我需要CRLF字符(Windows版本)

为此,在脚本末尾,我将LF转换为CRLF

例如:

TYPE file.txt | FIND "" /V > file_win.txt
del file.txt
rename file_win.txt file.txt

要批量启动新行,只需添加“echo[”,如下所示:

echo Hi!
echo[
echo Hello!

如果需要使用可传递给变量的著名\n字符串文字,可以编写如下Hello.bat脚本中的代码:

@echo off
set input=%1
if defined input (
    set answer=Hi!\nWhy did you call me a %input%?
) else (
    set answer=Hi!\nHow are you?\nWe are friends, you know?\nYou can call me by name.
)

setlocal enableDelayedExpansion
set newline=^


rem Two empty lines above are essential
echo %answer:\n=!newline!%
这样,多行输出可以在一个地方准备,甚至在其他Scriptpt或外部文件中,然后在另一个地方打印

换行符保存在换行符变量中。它的值必须在换行符展开后被替换,因此我使用setlocal enableDelayedExpansion启用感叹号,在执行时展开变量。执行用换行符内容替换\n(在帮助集中查找语法)。我们当然可以在设置答案时使用!newline!,但是\n更方便。它可能是从外部传递的(请尝试Hello R2\nD2),在那里没有人
echo Hi!
echo[
echo Hello!
@echo off
set input=%1
if defined input (
    set answer=Hi!\nWhy did you call me a %input%?
) else (
    set answer=Hi!\nHow are you?\nWe are friends, you know?\nYou can call me by name.
)

setlocal enableDelayedExpansion
set newline=^


rem Two empty lines above are essential
echo %answer:\n=!newline!%
:mlecho
setlocal enableDelayedExpansion
set text=%*
set nl=^


echo %text:\n=!nl!%
goto:eof
type nul | more /e /p
set "_line=hello world"
echo\%_line: =&echo;%
hello
world
set "_line=hello\nworld"
echo\%_line:\n=&echo;%
set nl=.
echo hello
echo%nl%
REM without space ^^^
echo World
hello
world