1

Why is it that in Python (3.6 in a Cygwin terminal running bash, on Windows 10)

>>> import subprocess
>>> subprocess.check_output(['echo', '"test"'])
b'\\test"\n'

NOTE: THE CONFUSING BIT IS THE LEADING BACKSLASH

To make that more clear:

>>> print(subprocess.check_output(['echo', '"test"']).decode())
\test"

Turns out Python2.7 gives me the expected result, but two different Python 3 installations give the wrong one.


In my understanding, subprocess.list2cmdline(x) should give something that is exactly equivalent when pasted in a terminal as subprocess.check_output(x). In this case though subprocess.list2cmdline(['echo', '"test"'])==r'echo \"test\"' and when pasting the latter into the terminal I do get the expected result

$ echo \"test\"
"test"

More tests:

>>> subprocess.check_output(subprocess.list2cmdline(['echo', '"test"']))
b'\\test"\n'

but

>>> subprocess.check_output(['echo', '"test"'], shell=True)
b'\\"test\\"\r\n'
>>> subprocess.check_output(subprocess.list2cmdline(['echo', '"test"']), shell=True)
b'\\"test\\"\r\n'
Bananach
  • 2,016
  • 26
  • 51

0 Answers0