Passing around multi-line strings

2019-01-21 22:26发布

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?

2条回答
老娘就宠你
2楼-- · 2019-01-21 23:21

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

查看更多
我命由我不由天
3楼-- · 2019-01-21 23:23

Expansion to jeb answer , Adding !LF!^ to each line would be easy

setlocal EnableDelayedExpansion
set LF=^



set multiLine=This is a!LF!^
multiline text!LF!^
line3
echo !multiLine! 
查看更多
登录 后发表回答