- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
You can execute terminal (shell) commands directly from Python using built-in modules like subprocess or os. The subprocess module is the recommended modern approach as it provides better control over input/output and error handling.
Example – Using subprocess.run() to list files:
import subprocess# Run 'ls -l' in the shellsubprocess.run("ls -l", shell=True, executable="/bin/bash")Copied!✕CopyThis executes the command in a Bash shell and prints the output directly to your terminal.
1. Using subprocess.run()
Best for running commands where you just need to execute and optionally check the exit status.
import subprocess# Execute a commandresult = subprocess.run("echo 'Hello from Python!'", shell=True, executable="/bin/bash")print("Exit Code:", result.returncode)Copied!✕CopyPros: Simple, supports shell pipelines, can specify executable.
Cons: Doesn’t capture output unless specified with capture_output=True.
2. Using subprocess.check_output()
Executing Shell Commands with Python - GeeksforGeeks
Feb 14, 2026 · This article starts with a basic introduction to Python shell commands and why one should use them. It also describes the three primary ways to run Python shell commands. os.system …
See results only from geeksforgeeks.orgEcho
The echo command in Linux is a built-in command that allows users to display …
PWD
Displaying the Current Working Directory Using Binary pwd (/bin/pwd): The binary …
Operating System
An operating system acts as an intermediary between the computer …
Sign In
This article starts with a basic introduction to Python shell commands and why one …
How do I execute a program or call a system command?
How do I call an external command within Python as if I had typed it in a shell or command prompt?
Usage examplesubprocess.call(["ls", "-l"])Python Subprocess Tutorial: Master run() and Popen() …
Learn how to use Python’s `subprocess` module, including `run()` and `Popen()` to execute shell commands, capture output, and control processes with real-world …
How to Execute a Bash Command in a Python Script
Feb 19, 2025 · Python is an open-source, interpreted, and object-oriented programming language. It’s one of the most popular programming languages …
How to Execute a Shell Command in Python [Step-by-Step]
Feb 22, 2021 · Executing a shell command in Python helps you create programs to automate tasks on your system. Learn how to do that now.
Searches you might like
Python: Running Shell Commands and Retrieving Output
Apr 6, 2025 · Python provides powerful mechanisms to run shell commands and capture their output. This ability is incredibly useful for system administration tasks, automating repetitive operations, and …
Run Shell Commands in Python and Get Output - PyTutorial
Feb 19, 2026 · Learn how to execute shell commands from Python using subprocess module, capture output, handle errors, and apply best practices for automation.
Executing Shell Commands with Python - Stack Abuse
Instead of shell scripts, which can get complex and tedious, we can use Python to automate shell commands. In this tutorial, we'll learn how to run them for the sake of scalability and maintainability of …
Python Run Bash Commands - milddev.com
Jul 23, 2025 · Learn how to run bash commands in Python using subprocess, asyncio, or third-party libraries, capturing output and handling errors.
How to run shell commands in Python? [SOLVED]
Jan 9, 2024 · There are different modules available which can be used to execute or call shell commands inside Python program.