how to use shlex in python with example

In Python, the shlex module provides a convenient way to split shell-like syntax into tokens. This can be particularly useful when dealing with command-line inputs or parsing configuration files. By using shlex, you can ensure that your program correctly handles whitespace, quotes, and escape characters. For example, suppose you have a string containing a command-line-like input: command = “ls -l /usr/bin“. Using shlex.split(), you can effortlessly split this string into tokens: tokens = shlex.split(command). This results in a list of tokens [‘ls’, ‘-l’, ‘/usr/bin’], ready for further processing or execution within your Python script.

To illustrate further, consider a scenario where you want to execute a command with arguments stored in a string variable. Using shlex, you can easily parse this string into separate elements.

import shlex

command = "grep -i 'hello world' file.txt"
tokens = shlex.split(command)

print("Command:", tokens[0])
print("Arguments:", tokens[1:])
shlex in python with example

The shlex.split() function splits the command string into individual elements, preserving quotes and spaces as expected. This makes it a valuable tool for handling complex command-line inputs within Python scripts.

Example 1

Shell Scripting in Python:

The shlex.split() is used to break down a shell command string (command) into individual elements, which are then executed using This approach enables Python scripts to execute shell commands safely and efficiently, handling complex command-line operations with ease.

import shlex
import subprocess

command = "ls -l | grep 'example'"
command_tokens = shlex.split(command)
result =, capture_output=True, text=True)

Example 2

Configuring Docker Containers:

The shlex.split() is utilized to parse a command string intended for execution within a Docker container. By splitting the command accurately, Python scripts can configure and interact with Docker containers seamlessly, ensuring proper handling of arguments and preserving quoted strings as expected.

import shlex
import docker

client = docker.from_env()
container ="ubuntu", detach=True, command=shlex.split("echo 'Hello, Docker!'"))

Example 3

Automating System Administration Tasks:

shlex.split() is employed to tokenize a series of shell commands (command) for execution using This approach is commonly used in system administration scripts to automate tasks such as file permission changes (chmod) and execution of custom scripts (./, streamlining system maintenance and management processes.

import shlex
import subprocess

command = "chmod +x && ./"
command_tokens = shlex.split(command)