Search code examples
pythonshellquoting

Python module to shellquote/unshellquote?


Is there anything in the Python standard library that will properly parse/unparse strings for using in shell commands? I'm looking for the python analog to perl's String::ShellQuote::shell_quote:

$ print String::ShellQuote::shell_quote("hello", "stack", "overflow's", "quite", "cool")
hello stack 'overflow'\''s' quite cool

And, even more importantly, something which will work in the reverse direction (take a string and decompose it into a list).


Solution

  • pipes.quote is now shlex.quote in python 3. It is easy enough to use that piece of code.

    https://github.com/python/cpython/blob/master/Lib/shlex.py#L281

    That version handles zero-length argument correctly.