windows 传递多行字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7041069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing around multi-line strings
提问by chief7
I'm trying to create a batch file which passes around a string with line feeds in it but its not working out. The continuation of the string is executed as a new command.
我正在尝试创建一个批处理文件,它传递一个带有换行符的字符串,但它不起作用。字符串的延续作为新命令执行。
Is there anyway to encode a line feed or make this work?
无论如何编码换行符或使这项工作?
回答by jeb
You can create directly multiline strings with the caret (one empty line is required).
您可以使用插入符号直接创建多行字符串(需要一个空行)。
setlocal EnableDelayedExpansion
set multiLine=This is a ^
multiline text^
line3
echo !multiLine!
Or you can create first a newline character.
或者您可以先创建一个换行符。
setlocal EnableDelayedExpansion
set LF=^
rem Two empty lines are required
set multiLine=This is a!LF!multiline text!LF!line3
echo !multiLine!
An explanation how this works can be found at Explain how dos-batch newline variable hack works
可以在解释 dos-batch 换行变量 hack 的工作原理中找到有关其工作原理的解释
回答by SmartManoj
Expansion to jeb answer, Adding !LF!^ to each line would be easy
扩展到jeb answer,将 !LF!^ 添加到每一行会很容易
setlocal EnableDelayedExpansion
set LF=^
set multiLine=This is a!LF!^
multiline text!LF!^
line3
echo !multiLine!