如何在批处理文件输出中插入换行符号?
我想要做这样的事情:
我想要做这样的事情:
echo hello\nworld
将输出:
hello
world
echo hello\nworld
将输出:
hello
world
使用:
echo hello
echo:
echo world
(echo hello & echo world) >> ./test.txt
- Ned Martinecho line 1 > Makefile
然后再执行 echo line 2 >> Makefile
。使用 >>
可以将输出附加到文件的末尾,这正是您在此情况下所需要的行为。 - dgnuffecho hello & echo.world
这意味着你可以将& echo.
定义为代表换行符\n
的常量。
echo. && echo hello && echo. && echo world
。 - matt wilkie$ (echo 你好 && echo 世界) > ./文件.txt
- Nathan J.B.echo(
,以提高性能(仍然不要有空格)。关于其他字符及其优点/缺点的详细讨论,请参见ECHO. FAILS to give text or blank line - Instead use ECHO/。 - matt wilkie好的,您可以创建一个 .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 语句之间的代码。
set nl=^&echo.
,然后只需输入echo hello %nl% world
即可产生相同的效果。 - user2605194在 cmd/bat 文件中有一个标准功能 echo:
可以输出空白行,这样可以模拟 cmd 输出中的新行:
@echo off
echo line1
echo:
echo line2
或者@echo line1 & echo: & echo line2
上述cmd文件的输出:
line1
line2
echo()
。它看起来可能会引起问题,但实际上完美地发挥作用。所有其他形式都至少有一种情况,在该情况下,命令不会按预期执行。 - dbenhamecho
命令时,有哪些字符可以紧随其后,以及它们的效果是什么?” - matt wilkie类似于Ken的回答,但使用延迟扩展。
setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)
echo Line1!\n!Line2
echo Works also with quotes "!\n!line2"
首先创建一个单个换行符并将其赋给\n变量。echo.
这不是答案,因为问题是关于能够输出多行的单个echo
。echo.
创建新行,但应注意,echo.
是最慢的,并且可能会完全失败,因为cmd.exe搜索名为ECHO
的文件并尝试启动它。echo,
echo;
echo(
echo/
echo+
echo=
但应避免使用echo.
、echo\
或echo:
,因为它们可能非常缓慢,取决于脚本执行的位置,如网络驱动器。
call :pause line1!\n!line2
,请在子例程中使用 setlocal EnableDelayedExpansion
开始,并以 setlocal DisableDelayedExpansion
结束,以防止换行符被过早解释。 - stevek_mccecho,
echo;
echo(
echo/
echo+
echo=
和反派角色集合 echo.
echo\
echo:
给予一颗加分。如果你能解释这些奇怪的东西为什么能够工作,我将欠你一瓶免费啤酒。 - Bob Steinnewline.bat
中,最好加上 echo(!\n!
。 - jebecho(!\n!
我的意图是在每个想要使用你的方法的批处理文件的顶部调用外部setup_newline.bat。或者,更好的办法是,在需要换行符的任何地方都放置“newline”,并让该“newline”调用“newline.bat”,返回“!\n!”。 - johny whyecho.
话不多说。
如果您需要在一行内显示,请使用 &
。例如:
echo Line 1 & echo. & echo line 3
将输出为:
Line 1
line 3
set n=^&echo.
echo hello %n% world
输出
hello
world
%n%
。这更接近各种语言中使用的\n
。set n=
将变量n
设置为:
^
清空接下来的符号:
&
表示在同一行上执行另一个命令。我们不关心错误级别(它是一个echo语句,不用大惊小怪),因此不需要&&
。
echo.
继续echo语句。set t=^&echo. ::there are spaces up to the double colon
echo.
,因为它首先会尝试找到一个同名的文件。使用其他标点符号,比如 echo,
。 - Kuba hasn't forgotten Monicaecho %n%hello%n%world
会失败,正确的写法是echo & echo:hello & echo:world
。 - Gerold Broser如果要将内容输出到文件中,多个“echo”命令是不会起作用的。我认为“>>”重定向符可能是一个不错的选择:
echo hello > temp echo world >> temp
如果您需要将结果写入文件,您可以使用:
(echo a & echo: & echo b) > file_containing_multiple_lines.txt
就像Grimtron建议的那样 - 这里有一个快速的示例来定义它:
@echo off
set newline=^& echo.
echo hello %newline%world
C:\>test.bat
hello
world
echo hello %newline%world
这一行,则之间没有空格。 - matt wilkieecho %newline%hello%newline%world
会失败,正确的写法是echo & echo:hello & echo:world
。 - Gerold Broser你也可以这样做:
(for %i in (a b "c d") do @echo %~i)
a
b
c d
(for %%i in (a b "c d") do @echo %%~i)
*
字符。 - user6250760echo a & echo b & echo c d
,并且不能用 echo a sentence that contains\na newline identifier in it
来回答问题。当然,可以写成 for %%i in ("a sentence that contains" "a newline in it") do @echo %%~i
,但真的需要吗? - Gerold Broser