← All notes

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 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

Sources

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

Permanent ID: iil:note:d3pfa1hg

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.},
}

Markdown

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.

All artifact files (JSON index)

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.