Both 'foo' and "'foo'" converted by shlex.split to the same list:
In [44]: shlex.split("'foo'") Out[44]: ['foo'] In [45]: shlex.split("foo") Out[45]: ['foo']
Therefore, I do not think that in all cases you can undo shlex.split , but this can close you:
In [20]: import subprocess In [21]: subprocess.list2cmdline(shlex.split('prog -s "foo bar"')) Out[21]: 'prog -s "foo bar"' In [22]: subprocess.list2cmdline(shlex.split('prog -s "foo bar" "baz"')) Out[22]: 'prog -s "foo bar" baz'
source share