1

I have a python script that takes its input from the command line arguments. For example:

./myscript.py first_item second\ item "third item"

I can output separate items and escape spaces and special characters using pipes.quote.

print " ".join(map(pipes.quote, outputItems))

Is there any existing "unquote" interface that will parse a bash argument string, keeping escaped spaces and quoted strings intact?

Something that would allow the same python script to handle this:

echo 'first_item second\ item "third item"' | ./myscript.py
1
  • Note that this is what xargs did, and it turned out to be a horrible design decision that people still suffer from 30 years later. It's generally agreed that \0 terminated strings, or at the very least \n terminated, is the way to go. Commented Mar 12, 2014 at 16:17

1 Answer 1

4

You want shlex.split():

s = 'first_item second\ item "third item"'

import shlex

shlex.split(s)
Out[3]: ['first_item', 'second item', 'third item']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.