Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. 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 shell
    subprocess.run("ls -l", shell=True, executable="/bin/bash")
    Copied!

    This 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 command
    result = subprocess.run("echo 'Hello from Python!'", shell=True, executable="/bin/bash")

    print("Exit Code:", result.returncode)
    Copied!
    • Pros: Simple, supports shell pipelines, can specify executable.

    • Cons: Doesn’t capture output unless specified with capture_output=True.

    2. Using subprocess.check_output()

    Feedback
  2. 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 example
    subprocess.call(["ls", "-l"])
  3. 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 …

  4. 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 …

  5. 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.

  6. 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 …

  7. 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.

  8. 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 …

  9. 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.

  10. 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.