您可以创建一个真正的换行符并将其分配给一个变量。
setlocal EnableDelayedExpansion
set LF=^
rem TWO empty lines are required
echo This text!LF!uses two lines
换行符最适合延迟扩展,您也可以将其与百分比扩展一起使用,但它有点复杂。
set LF=^
rem TWO empty lines are required
echo This text^%LF%%LF%uses two lines
echo This also^
uses two lines
怎么运行的?
插入符号是转义字符,它会转义下一个字符,并且自身会被删除。
但是,如果下一个字符是换行符,则换行符也会被删除,并且只有下一个字符被有效转义(即使这也是换行符)。
因此,需要两个空行,LF1 被忽略,LF2 被转义,LF3 是完成“行”所必需的。
set myLinefeed=^<LF1>
<LF2>
<LF3>
Hints:
使用完全不同的换行变量定义格式通常会更好,
以避免无意中删除所需的空行。
(SET LF=^
%=this line is empty=%
)
我经常删除其中一个空行,然后我一直在寻找为什么我的程序不再工作。
偏执版本还会检查换行符变量中是否有空格或其他垃圾。
if "!LF!" NEQ "!LF:~0,1!" echo Error "Linefeed definition is defect, probably multiple invisble whitespaces at the line end in the definition of LF"
FOR /F "delims=" %%n in ("!LF!") do (
echo Error "Linefeed definition is defect, probably invisble whitespaces at the line end in the definition of LF"
)