I want to split a string into command-line arguments, exactly like shlex.split does. However, shlex doesn't seem to convert environment variables (for example $USER
), and the output makes it impossible to know whether the environment variable was escaped:
>>> print(shlex.split("My name is Alice"))
['My', 'name', 'is', 'Alice']
>>> print(shlex.split("My name is '$USER'"))
['My', 'name', 'is', '$USER']
>>> print(shlex.split("My name is $USER")) # expected Alice, not $USER
['My', 'name', 'is', '$USER']
Is there a way to achieve this? (hopefully without re-implementing the whole thing)
Also, why doesn't shlex.split do this by default in the first place?
If it matters, I am using Python 3.6.8.