Splitting a command line with str.split() breaks arguments that contain spaces or quotes. shlex.split() follows Unix-like shell rules and shlex.join() creates a readable representation.
Practical example
import shlex
command_line = 'deploy --message "stable release" --dry-run'
arguments = shlex.split(command_line)
print(arguments)
print(shlex.join(arguments))
Arguments are not code
When launching a program, prefer an argument list with subprocess and shell=False. Use shlex to parse a syntax your application intentionally supports, not to expose a full shell.
Portability and external input
Quoting rules target POSIX shells and do not match every environment, particularly Windows. A correctly quoted string can still request a dangerous operation, so validate the program, options, and allowed values too.
Keep learning
Continue with Python subprocess: Run Commands Safely and Python Argparse: Build a CLI with Subcommands. A official Python documentation, accessed July 22, 2026, documents the API and behavior across versions.