Open In App
Related Articles

How to run bash script in Python?

Improve Article
Save Article
Like Article

If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python code. The two common modules for interacting with the system terminal are os and subprocess module.

Let’s consider such a simple example, presenting a recommended approach to invoking subprocesses. As an argument, you have to pass the command you want to invoke and its arguments, all wrapped in a list.

Running simple bash script on terminal using Python


import os
os.system("echo GeeksForGeeks")



Executing bash scripts using Python subprocess module

A new process is created, and command echo is invoked with the argument “Geeks for geeks”. Although, the command’s result is not captured by the python script. We can do it by adding optional keyword argument capture_output=True to run the function, or by invoking check_output function from the same module. Both functions invoke the command, but the first one is available in Python3.7 and newer versions.


import subprocess
# From Python3.7 you can add
# keyword argument capture_output
print(["echo", "Geeks for geeks"],
# For older versions of Python:
                               "Geeks for geeks"]))


CompletedProcess(args=['echo', 'Geeks for geeks'], returncode=0, stdout=b'Geeks for geeks\n', stderr=b'')
b'Geeks for geeks\n'

Execute an existing bash script using Python subprocess module

We can also execute an existing a bash script using Python subprocess module.


import subprocess
# If your shell script has shebang,
# you can omit shell=True argument.
                "arguments"], shell=True))


CompletedProcess(args=['/path/to/your/shell/script', 'arguments'], returncode=127)

Common problems with invoking shell script and how to solve them:

  • Permission denied when invoking script — don’t forget to make your script executable! Use chmod +x /path/to/your/script
  • OSError: [Errno 8] Exec format error — run functions lacks shell=True option or script has no shebang.

Executing bash command using Python os module

Using os module also, we can execute bash command. Here, we have executed a bash command to find the current date.


import os
os.system('date +"Today is: %A %d %B"')


Today is: Friday 02 September

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Sep, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials