2

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.

Gus
  • 4,375
  • 5
  • 31
  • 50

1 Answers1

0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • This does not seem to work. I've edited the question to respond with formatting. – Gus Jul 18 '22 at 15:45
  • Note @Gus, you shown the exact multiline output you wanted to send. However your single line version had a semicolon, which was missing from what you specifically asked for. I have now added that to my answer to match the single line version. – Compo Jul 18 '22 at 16:53
  • Hi @Compo, The semicolon is not needed if there is a newline in the input to Python. I still do not see the expected output of '5.0'. Do you? – Gus Jul 18 '22 at 20:07
  • You are passing a single command line argument to python, using my suggestion, @Gus, _(the same command line as in your single line example)_. Therefore in order for python to see `import math` as a separate command to `print(math.hypot(3, 4))`, I'd assume that the semicolon is needed to tell python that there are two commands, _(the semicolon separates those two as if on two individual lines)_. I've no idea whether it works for python, as I wouldn't use it on a non Unix system, but the technique works for other quoted commands I've had the need to split over multiple in a Windows batch file. – Compo Jul 18 '22 at 23:07
  • I want to pass in two arguments to python: 1. `-c`, 2. the script. Furthermore, I would like the script to contain newlines in it. I replaced "python" with "echo", and I expect to see printed "-c" and then the script, however nothing is printed. Do you get this behavior too? – Gus Jul 19 '22 at 16:55