如何在批处理文件输出中插入换行符号?
我想要做这样的事情:
我想要做这样的事情:
echo hello\nworld
将输出:
hello
world
echo hello\nworld
将输出:
hello
world
这对我有用,不需要延迟扩展:
@echo off
(
echo ^<html^>
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause
它会输出以下内容:
<html>
<body>
Hello
</body>
</html>
Press any key to continue . . .
echo asdf >myfile.txt
会产生完全相同的结果。echo
命令在字符串末尾添加一个换行符。 - BlueRaja - Danny Pflughoeft你可以使用@echo
(@echo + [空格] + [不间断空格])
注意:不间断空格可以通过Alt+0160获得
希望能帮到你 :)
[编辑] 嗯,你说得对,我需要在Makefile中使用它,在那里它完美地工作。我想我的答案不适用于批处理文件...我的错。
在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
日志和屏幕输出如下所示...
请注意,这段代码在控制台中不起作用,因为它会模拟按下“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!
,因为在底部已经没有空间来向下移动光标,所以它只会向左移动(使用 CRLF
的 CR
)并覆盖 Hello
。证明:使用 echo Hello my%\n%world!
将显示 world!my
。无论如何,供参考:Console Virtual Terminal Sequences, Cursor Positioning。 - Gerold Brosersetlocal EnableDelayedExpansion
)set text=hello\nworld
set "text=%text:\n= & echo %"
(echo %text%) > file.txt
cat file.txt
简单
set nl=.
echo hello
echo%nl%
REM without space ^^^
echo World
结果:
hello
world
你可以使用 printf
代替 echo
:
printf "hello\nworld"
输出:
hello
world