How To Run Bash Scripts Using Python?

February 15, 2023
how To Run Bash Scripts Using Python



Linux developers’ love for shell commands is no news. Moreover, those who work with Python also run bash scripts now and then for automation. Python is a super advanced bash with added functionality and lesser hassle. So, it is natural to prefer Python over Bash.

However, a time comes when you have bash scripts prepared yet have to move to Python. What do you do? Throw them in a bin and start anew. No, there is a way to run bash scripts in Python, and we will tell you how.

Fortunately, Python has a built-in module designed to follow the exact purpose called subprocess. Let’s learn how to use it to run bash scripts in Python.

python CTA

Bash Python Execution 101

Many methods and classes within the subprocess module exist to execute bash scripts in Python. Among the most important ones are run and Popen.

subprocess.run()

The method processes a strings list as a positional argument. The first entry is the command name, and arguments follow next.

For instance,

     import subprocess
     subprocess.run ( [ “ls”] )

The above command lists all the items present in the script’s directory. Note that it is only a bash command with no arguments attached. Additional arguments, like –a, -l, -la, etc., can also be added. Here’s how:

     import subprocess
     subprocess.run ( [ “ls”, “-la”] )

The –la command displays all files in the directory, including the hidden ones and permissions. It is common to make mistakes, especially when working with bash commands. To capture them, we use stderr, a keyword argument.

subprocess.run() – input

The input argument gives input to bash commands. Moreover, it is best to couple it with another argument text and set it to True, as, by default, it processes them in bytes. Here’s an example.

     import subprocess
     subprocess.run ( [ “python3”, “add.py”], text=True, input=”3 4” ) 

subprocess.Popen()

A class more advanced than the method above, granting developers more flexibility to execute bash commands. It helps check the command’s status, give input, get output, and process other similar functions. However, it is best to learn a couple of additional methods first.

Communicate

This method gets you the output & error and also instructs your command via input. You will get a tuple in return with output and error. Here’s an example.

import subprocess
process = subprocess.Popen (["echo", "Hello, World!"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
result = process.communicate( )
print(result)

Wait

This method stalls execution until the completion of the command above. In other words, it restricts Python from executing the line below till the line above wait gets executed. Here’s an example.

import subprocess
process = subprocess.Popen(["ls", "-la"])
process.wait( )
print("Completed!")

Run this code without the wait command, and you will notice the print statement above the rest. However, adding a wait command between solves this and delays the print statement till the execution of the lines above.

How To Execute Bash Scripts in Python?

The two ways explained above help run bash commands. Now, let’s turn to how we can run bash scripts in Python.

Firstly, it is necessary to build familiarity with another method in the subprocess called call. That’s the one you use to execute bash scripts. Remember, the default code in Python for bash scripts is 0. Here’s a bash script.

#!/bin/bash


echo "Hello, World!"

exit 1

Here’s the python script to execute the above bash script named sample.sh.

import subprocess

exit_code = subprocess.call('./sample.sh')

print(exit_code)

Output:

Hello, World!

1

Wrap Up

Python developers have a lot on their plate, and the skilled ones seldom miss a chance to automate. That said, it is not uncommon to run into bash scripts while you sit to code a program. At such times, trashing the outdated script for a better Python one is not the cure. Instead, the solution is to learn how to run the same bash script in Python to save time and enhance productivity.

In this article, you learned how to run bash scripts in Python. Remember, the module to know is the subprocess. The module further divides into methods and classes through which you can execute bash commands in the desired way in Python. The most common ones are above. So, time to automate!

Also Read: Guide To Integration Testing In Python

python CTA2



author

jordan

Full Stack Java Developer | Writer | Recruiter, bridging the gap between exceptional talent and opportunities, for some of the biggest Fortune 500 companies.


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US