Windows Batch File, build multiline string literal across multiple lines

Viewed 53

I have a batch file (called single-line.bat) with the following contents

python -c "import math; print(math.hypot(3, 4))"

I want the argument to python to be a multiline string that is constructed across multiple lines of the batch file. That is, I'd like the contents to resemble this

python -c "import math
print(
math.hypot(
3,
4)
)"

That is, I want to build a string literal that contains multiple lines. I also want to build that string literal across multiple lines.

This answer builds a command across multiple lines, not a string literal: https://superuser.com/a/1025263

This answer builds a string across multiple lines, but the string itself does not contain multiple lines: https://superuser.com/a/1596233

This answer comes close, but since it does not use quotes, it looks like I to escape batch characters such as %.: Passing around multi-line strings

To be sure, the following is a valid python script:

import math
print(
math.hypot(
3,
4)
)

and that is the script that I want to pass as an argument to the -c flag of the python command in my batch file.

[EDIT]

An answer suggests making a file proposal-1.bat

python -c "import math"^
 "print("^
 "math.hypot("^
 "3,"^
 "4)"^
 ")"

This doesn't work. Compared to the original single-line.bat:

> single-line.bat

> python -c "import math; print(math.hypot(3, 4))"
5.0

> proposal-1.bat

> python -c "import math" "print(" "math.hypot(" "3," "4)" ")"

I do not see any output python.

1 Answers

I cannot successfully post this as a comment with the intended formatting, so here it is as a potential answer instead.

Have you tried:

python -c "import math;"^
 "print("^
 "math.hypot("^
 "3,"^
 "4)"^
 ")"

Please note that the leading spaces on the continuation lines are intentional.

Related