In this article, you'll learn how to run a Linux command or shell script from a Python script, capture their output into a Python variable, and check their execution status.
How One Can Execute Linux Command From Python Script
There are many different ways to run a Linux command from a Python script without requiring any additional tools or external libraries, as you can run it using the Python built-in libraries.
I've listed the most common and well-known below:
Python Library | Description |
---|---|
os.system | You can run your Linux command with arguments on your system, allowing you to execute single or multiple commands simultaneously through pipe or input/output redirection. However, the downside is that you must manually handle shell character escaping, and it only permits the execution of shell commands, not external programs. |
os.popen | This is identical to the above command, except it gives you a file-like object that you can use to access standard input/output for that process. If you pass everything as a string, then your command is passed to the shell, but if you pass them as a list, then you don't have to worry about escaping anything. |
subprocess.Popen | This was introduced as a replacement for the above command, but the complexity of this command became its downside. |
subprocess.call | It's entirely identical to the above command, but it waits until the command completes and provides you with the return code. |
subprocess.run (recommended) | This command is only available for Python 3.5+, and I consider it to be the best among all those mentioned. The reason is that it gives you more flexibility and returns a CompletedProcess object when the command finishes executing. |
As you can see, there are multiple ways to run a Linux command in Python, but for this tutorial, I'm going to use the subprocess.run
command because it's built for the latest Python v3 and has eliminated many hurdles while introducing new features.
Execute Linux Command From Python Script
To execute the Linux command from a Python script, we will use the subprocess.run
command. But before that, you need to first create a Python file with any name (I've chosen program.py
) and copy-paste the following line of code into that file.
import subprocess
subprocess.run(["echo", "Ubuntu Shell"])
In the above code, we're just importing the subprocess
built-in Python library and then using the subprocess.run
command to execute the Linux echo command with "Ubuntu Shell" as a parameter.
After saving and closing the file, running it with the python3
command will give the output of the Linux command.
Additionally, if you wish to execute a Linux command with parameters, you can do so. For example, executing the Linux ls command with the -l
flag can be implemented like below:
import subprocess
subprocess.run(["ls", "-l"])
Output:
Now, let's see how one can execute a shell script from a Python script.
Execute Linux Shell Script From Python Script
Executing a Linux shell script from a Python script is almost identical to executing a Linux command, as discussed before. Allow me to explain why: The following is a basic shell script saved with the filename script.sh
, which takes one input from the user and then returns it as output.
#!/bin/bash
# Ask the user to enter their name
echo "Please enter your name:"
read name
# Print the user's name
echo "Hello, $name!"
To execute this shell script from a Python script, all you need to do is specify the sh
command with the script's relative or absolute path as an argument to the subprocess.run
command for execution.
import subprocess
subprocess.run(["sh", "./script.sh"])
Output:
Now, let's see how you can get the exit status code of a Linux command or script file executed from a Python script.
Get the Exit Status Code of Linux Command or Script File Executed from Python Script
To get the execution status of a Linux command or script file, you can use special keywords such as stdout
, stderr
, and returncode
by assigning the subprocess.run
line to a Python variable and then using the variable with the mentioned special keywords to get the desired result:
For example, the following is a Python script file that runs the Linux echo
command and then returns its output, including its execution status, such as stdout
, stderr
, and returncode
.
import subprocess
process = subprocess.run(["echo", "Ubuntu Shell"])
print(process.stdout);
print(process.stderr);
print(process.returncode);
Output:
Finally, let's see how one can assign the output of a Linux command or shell script to a Python variable and later use it in their Python program.
Assigning Linux Command or Shell Script Output to a Python Variable
Assigning Linux command or shell script output to a Python variable is very easy, but what happens if you directly try to assign the subprocess.run
line to a Python variable and try to use it with other Python functions (let's say print()
)?
As you can see, we got the output, but it wasn't what we expected. The Linux command specified to subprocess.run
is echo "Ubuntu Shell"
, which outputs Ubuntu Shell
, and we aim to save this output to a Python variable.
So, hmm, what can one do? Don't worry, all you need to do is add this capture_output=True, text=True
line inside the subprocess.run
function. Then, because the output will have a stripped result, you can add .stdout.strip("\n")
at the end of the closing round bracket.
After applying all the changes to your Python file, it will look like the one below:
import subprocess
process = subprocess.run(["echo", "Ubuntu Shell"], capture_output=True, text=True).stdout.strip("\n")
print(process)
print(process)
Output:
That's it. This way, you can save your Linux command or shell script output to a Python variable.