The `shellescape` Python module provides the `shellescape.quote()` function, which is a backport of Python 3.8's `shlex.quote()` functionality. It safely escapes strings for use as single tokens within shell commands, mitigating shell injection vulnerabilities when executing external commands from Python scripts. The library is currently active, with its latest release `v3.8.1` in January 2020.
pip install shellescapeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `quote` function and use it to escape user-provided input. The escaped string can then be safely embedded as a single token in a shell command string. While `shellescape` facilitates constructing shell command strings, the general best practice for executing external commands in Python is to pass arguments as a list to `subprocess.run()` (which defaults to `shell=False`) to avoid shell interpretation entirely.
On Python 3.3 and newer, use `from shlex import quote` directly. Only use `shellescape` if targeting environments where `shlex.quote` is not present.
Always pass arguments as a list to `subprocess.run()` (e.g., `subprocess.run(['command', arg1, arg2])`). If `shell=True` is strictly required, ensure *every* user-controlled component in the command string is individually escaped using `shellescape.quote()` or `shlex.quote()`.
Review any code that relies on `shellescape.quote()`'s exact output, especially when upgrading from versions prior to `3.8.1`, and test against the new behavior.
pip install shellescape
Install the `shellescape` library (`pip install shellescape`) and use `shellescape.quote()` instead.
Ensure the input to `shellescape.quote()` is a string, bytes, or bytearray object. Convert other types to a string if necessary (e.g., `str(value)`).
No dependency data recorded yet.