| name | shell-scripting |
| description | Execute shell commands from Python. Use for running CLI tools, scripts, or system commands. |
Shell Scripting
Execute shell commands and scripts from Python.
Quick Start
import subprocess
# Simple command
result = subprocess.run(["ls", "-la"], capture_output=True, text=True)
print(result.stdout)
# With shell expansion
result = subprocess.run("echo $HOME", shell=True, capture_output=True, text=True)
Common Patterns
Run and capture output
def run_command(cmd: list[str]) -> tuple[str, str, int]:
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout, result.stderr, result.returncode
stdout, stderr, code = run_command(["python", "--version"])
Check command success
def command_exists(cmd: str) -> bool:
try:
subprocess.run(
["which", cmd],
capture_output=True,
check=True
)
return True
except subprocess.CalledProcessError:
return False
Run with timeout
try:
result = subprocess.run(
["long-running-command"],
timeout=30,
capture_output=True,
text=True
)
except subprocess.TimeoutExpired:
print("Command timed out")
Pipe commands
# Equivalent to: cat file.txt | grep pattern
p1 = subprocess.Popen(["cat", "file.txt"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(
["grep", "pattern"],
stdin=p1.stdout,
stdout=subprocess.PIPE,
text=True
)
output = p2.communicate()[0]
Run in specific directory
result = subprocess.run(
["npm", "install"],
cwd="/path/to/project",
capture_output=True,
text=True
)