Python shlex - split shell-like strings and quote arguments safely
Answer: Use shlex.split(s) to split the string s using shell-like syntax, shlex.quote(s) to make one value safe as a single shell token, and shlex.join(list) to turn a token list back into a shell-escaped string. Prefer passing a list to subprocess.run() with shell=False when you can.
Examples
import shlex
shlex.split("ls -l 'my file.txt'")
# ['ls', '-l', 'my file.txt']
shlex.join(['echo', '-n', 'Multiple words'])
# "echo -n 'Multiple words'"
filename = 'somefile; rm -rf ~'
command = 'ls -l {}'.format(shlex.quote(filename))
# "ls -l 'somefile; rm -rf ~'"
Functions
| Function | Returns | |---|---| | split(s, comments=False, posix=True) | a list of tokens; comments are not parsed unless comments=True | | join(split_command) | the tokens joined into one shell-escaped string, the inverse of split() | | quote(s) | a shell-escaped version of the string, safe as one token in a shell command line |
Why quote
Putting a filename like 'somefile; rm -rf ~' straight into a command would be unsafe when executed by a shell. quote() lets you plug the security hole, and the quoting is compatible with UNIX shells and with split().
Limits
- The shlex module is only designed for Unix shells.
quote()is not guaranteed to be correct on non-POSIX shells such as Windows, where quoted commands can open up a command injection vulnerability. split()operates in POSIX mode by default;posix=Falseswitches to non-POSIX mode.- Since Python 3.12, passing
Nonetosplit()raises an exception rather than readingsys.stdin.
The lexer class
shlex.shlex(instream, posix=False, punctuation_chars=False) is a lexical analyzer object for custom mini-languages. In POSIX mode it follows POSIX shell parsing rules as closely as possible, and punctuation_chars=True treats characters like ();<>|& as separate tokens.
See also
Claims
- id: C1 claim: join is the inverse of split. source: S1 evidence: "This function is the inverse of split() ."
- id: C2 claim: shlex is only designed for Unix shells. source: S1 evidence: "The shlex module is only designed for Unix shells ."
- id: C3 claim: quote returns a shell-escaped version of a string. source: S2 evidence: "Return a shell-escaped version of the string s."
Sources
- [S1] shlex - Simple lexical analysis. Python docs. https://docs.python.org/3/library/shlex.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/shlex.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/shlex.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, shlex - Simple lexical analysis; python/cpython (GitHub), Lib/shlex.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, shlex - Simple lexical analysis; python/cpython (GitHub), Lib/shlex.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python shlex - split shell-like strings and quote arguments safely." iinventedlight Library, iil:note:d3pfa1hg, version of 2026-11-18. https://iinventedlight.com/library/notes/python-shlex/. License: CC-BY-4.0.
@misc{iil_note_d3pfa1hg,
author = {{iinventedlight}},
title = {{Python shlex - split shell-like strings and quote arguments safely}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-shlex/},
note = {Permanent ID iil:note:d3pfa1hg. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-shlex.table.json (table; CC-BY-4.0)
- python-shlex.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-shlex.md.