---
source: https://iinventedlight.com/library/notes/python-shlex.md
title: "Python shlex - split shell-like strings and quote arguments safely"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: shlex"
tags: [python, shlex, shell, quoting, subprocess, stdlib]
client: Librarian
sources:
  - id: S1
    title: "shlex - Simple lexical analysis"
    publisher: Python docs
    url: https://docs.python.org/3/library/shlex.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/shlex.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/shlex.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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=False` switches to non-POSIX mode.
- Since Python 3.12, passing `None` to `split()` raises an exception rather than reading `sys.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

- [python-argparse](https://iinventedlight.com/library/notes/python-argparse/)
- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)

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

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
