如何在批处理文件中使用echo输出换行符?

779
如何在批处理文件输出中插入换行符号?
我想要做这样的事情:
echo hello\nworld

将输出:

hello
world

9
对我很有用。我需要在脚本中运行 echo \n \n | my_app.exe。我运行了 (echo. && echo.) | my_app.exe。 - Vignesh
2
简单方法 "代码开始:" > echo hello&echo world,将给您所需的内容 - prudviraj
2
您可以在单独的一行上插入一个不可见的ASCII字符(255),这将强制产生一个空白新行。按住[alt]键,然后在小键盘上按下255。这将插入chr(255),它是一个空白方块。即"echo (alt+255)"。您只能使用小键盘,而不能使用querty键盘上面的数字键! - jwzumwalt
1
@jwzumwalt,感谢您提供的alt+255建议,在命令行上非常有效。 - K Vij
printf "\n\n\n" - Mark
显示剩余3条评论
27个回答

1

这对我有用,不需要延迟扩展:

@echo off
(
echo ^<html^> 
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause

它会输出以下内容:

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

5
使用echo asdf >myfile.txt会产生完全相同的结果。echo命令在字符串末尾添加一个换行符。 - BlueRaja - Danny Pflughoeft
2
很棒的答案,你是唯一一个认识到使用括号来换行的价值的人。我相信这就是 DOS 创造者想让你这样做的方式。 - djangofan

0

你可以使用@echo(@echo + [空格] + [不间断空格])

注意:不间断空格可以通过Alt+0160获得

希望能帮到你 :)

[编辑] 嗯,你说得对,我需要在Makefile中使用它,在那里它完美地工作。我想我的答案不适用于批处理文件...我的错。


我在命令提示符尝试时收到了“ECHO ist eingeschaltet (ON)”,而不是空行。 - jeb
4
这是一个不换行的空格,不是换行符。 - Ry-

0

在Ken的答案中添加一个变体,展示如何设置具有换行符的环境变量的值。

我们使用这种方法将错误条件附加到VAR中的字符串中,然后在所有错误检查结束时将其输出到文件中,作为所有错误的摘要。

这不是完整的代码,只是一个例子。

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: the two blank lines are required!
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
:: Example Usage:

Set ErrMsg=Start Reporting:
:: some logic here finds an error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title1!NL!Description!NL!Summary!NL!

:: some logic here finds another error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title2!NL!Description!NL!Summary!NL!

:: some logic here finds another error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title3!NL!Description!NL!Summary!NL!

echo %ErrMsg%
pause
echo %ErrMsg% > MyLogFile.log

日志和屏幕输出如下所示...

Log output of the script

Screen output of the script


如果您使用延迟扩展,我看不出仍然需要使用百分号扩展和换行符的任何理由。使用延迟扩展,您只需要一个简单的NL定义,变量的回显也很简单。 - jeb

0

请注意,这段代码在控制台中不起作用,因为它会模拟按下“Esc”键并清除该行。

使用此代码,请将<ESC>替换为0x1b转义字符或使用此Pastebin链接

:: Replace <ESC> with the 0x1b escape character or copy from this Pastebin:
:: https://pastebin.com/xLWKTQZQ

echo Hello<ESC>[Eworld!

:: OR

set "\n=<ESC>[E"
echo Hello%\n%world!

只要命令行没有到达控制台窗口的底部,这个方法就可以工作!然后只会显示 world!,因为在底部已经没有空间来向下移动光标,所以它只会向左移动(使用 CRLFCR)并覆盖 Hello。证明:使用 echo Hello my%\n%world! 将显示 world!my。无论如何,供参考:Console Virtual Terminal Sequences, Cursor Positioning - Gerold Broser
该死,你说得对。这真是令人失望。 - Vopel

0
要将这个写入文件,我找到的唯一好的选择是。不幸的是,我在if语句中遇到了困难(如果你也遇到了问题,当然我使用了setlocal EnableDelayedExpansion
set text=hello\nworld
set "text=%text:\n= & echo %"
(echo %text%) > file.txt
cat file.txt

0

简单

set nl=.
echo hello
echo%nl%
REM without space ^^^
echo World

结果:

hello
world

4
这个13年前的回答相比,你的回答过于复杂且没有必要。与其他解决方案一样,它并没有回答如何使用单个echo来完成操作的问题。请问有何不同? - jeb

0

你可以使用 printf 代替 echo

printf "hello\nworld"

输出:

hello
world

2
"printf" 不是有效的 cmd/batch 命令。 - Stephan
这对我有效!! - Muhammed_G
printf 在 Linux 上可用,但问题标记了 Windows。 - thetillhoff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接