@echo off
set NLC=^
set NL=^^^%NLC%%NLC%^%NLC%%NLC%
Usage:
echo There should be a newline%NL%inserted here.
我在此处查看了关于Stackoverflow的this文章,但并没有完全解释困扰我的那部分。我了解我们可以将其用作echo The newline is added^%NLC%%NLC%
。我搜索答案的大多数地方都显示了用法,但没有解释。
但是我想知道的是NL=^^^%NLC%%NLC%^%NLC%%NLC%
。我似乎无法理解幕后发生的一切,这很困扰我。我们为什么不能只做NL=^%NL%%NL%
?然后将其用作echo newline is%NL%before this
。
编辑:我一直在做一些测试,但是我似乎无法完全掌握正在发生的事情。这是我做过的测试:
@echo off
REM Creating a newline variable (the two blank lines are required!)
set NLC=^
REM This prints fine. But looks long and ugly.
echo test1^%NLC%%NLC%test2
REM OUTPUT:
REM test1
REM test2
REM Escape the %NLC%
set NL=^%NLC%%NLC%
REM This prints 'test3' but then the parser stops because it encoutners a newline(%NL% is not escaped)
echo test3%NL%test4
REM OUTPUT:
REM test3
REM We can escape the %NL%. This works fine, but this looks long and ugly again.
echo test5^%NL%%NL%test6
REM OUTPUT:
REM test5
REM test6
REM Substitute ^%NLC%%NLC% for %NL%. This prints "test7^" but then parser stops because the first %NLC% is not escaped.
REM The first "^" in "^^" escapes the second. So we need to escape the first caret in "^^"
echo test7^^%NLC%%NLC%^%NLC%%NLC%test8
REM OUTPUT:
REM test7^
这是令我困惑的部分。为什么echo test9^^^%NLC%%NLC%^%NLC%%NLC%test10
产生与以下结果不同的结果
SET tman=^^^%NLC%%NLC%^%NLC%%NLC%
echo test11%tman%test12
?后者不会创建空白行。
REM First caret escapes the second, so the second is a literal and will be printed.
REM The third caret escapes the "%NLC%%NLC%" so a we move to a newline.
REM The 4th caret escapes the second group of "%NLC%%NLC%", creating a blank line.
REM This prints "test9^". Then prints two newlines (creates a blank line).
echo test9^^^%NLC%%NLC%^%NLC%%NLC%test10
REM OUTPUT:
REM test9^
REM
REM test10
REM This prints fine. Why? Why is no extra caret printed here unlike the previous?
REM Why doesn't this create a blank line as it did in above example?
SET tman=^^^%NLC%%NLC%^%NLC%%NLC%
echo test11%tman%test12
REM OUTPUT:
REM test11
REM test12