How to run Bash commands in Python?

Bash commands in Python can be a powerful tool for automating tasks such as setting up development environment and or deploying code to server, and also integrating different tools and languages. So, In this Python article, we will learn to run Bash commands in Python Programming language. But before that a quick introduction about bash and Bash commands.

Table Of Contents

What is Bash?

Bash stands for Bourne-Again Shell which is another example of Shell. Bash is a Unix shell, which is a command-line interface for interacting with an operating system, and it is a popular shell on many Linux and Unix systems.

Here are some examples of common Bash commands:

  • ls: List the files in a directory
  • cd: Change the current directory
  • mkdir: Create a new directory
  • cp: Copy a file or directory
  • mv: Move a file or directory
  • rm: Remove a file or directory
  • echo: Print a message to the terminal

Bash also provides a number of powerful features, such as command line editing, command history, and shell scripting, which allow you to automate tasks and customize your shell environment.

Now Lets learn about some methods which can be used to run Bash commands in Python.

Method 1 : Using OS module

We can use some functions of OS module to run Bash commands in Python. OS module comes pre-installed with python programming language and it is the one of the most used module for interaction with system.

os.system()

This os.system() is used to execute commands which is in the string format in Shells of different operating systems by creating a subshell. This method is implemented by using Standard C function system().
– Parameter : This receives a command in string format.
– SYNTAX : os.system(command)
– Return : On Windows, this returns the value returned by the system shell. On UNIX, this returns the exit status of the process.

See the example code below:

CODE :

import os

# this will create a new directory.
os.system('mkdir shellfile')

# this will list all the subdirectories
# in the directory.
os.system('ls')

This will create a new directory and then execute the second Bash commands, which will list all the subdirectories in the working directory.

Output

test1.md  test2.md  test3.md  shellfile  temp.py

os.popen()

Also we can use another method of os module which is os.popen(). This method opens a pipe of command and this pipe then sends the output to another command.

SYNTAX :

os.popen(command)

Here is an example code and output

CODE :

import os

# this will create a directory os in the working directory.
os.popen('mkdir os')

# this lists all the directories in the working directory.
output = os.popen('dir').read()

print(output)

OUTPUT :

test1.md  test2.md  test3.md  os        temp.py
test4.md

Method 2 : Using subprocess module

Another method which can be used to run Bash commands in Python is by using the subprocess module. The subprocess module comes pre-installed with Python programming language and is mostly used to execute system commands. It has some features which provide edge over the os module like, this connects to input/output/error pipes and obtains their return codes.

subprocess.call()

The subporocess.call() method is used to initiate a program. This method calls/executes the command passed as an argument in the call() function. Here an array of commands can also be passed if you need to provide options and the arguments along with the shell command.

SYNTAX :

subprocess.call(args, stdin=None,stdout=None,stderr=None,shell=False)
  • args : Command that needs to be executed.
  • stdin : Standard input stream value which will be passed as os.pipe()
  • stdout : Standard output stream value which is obtained.
  • stderr : Handles error occurred from standard error stream.
  • shell : Boolean, If True executes the program in new shell.

See the example code below:

CODE :

# importing subprocess module
import subprocess

# using subprocess to create a new directory using the mkdir command.
subprocess.call('mkdir subprocess', shell=True)

# listing all the directories.
subprocess.call("ls",shell=True)

OUTPUT :

test1.md  test2.md  test3.md  test4.md subprocess  temp.py

subprocess.Popen()

The Popen() function is an upgraded version of call() method of subprocess module which takes a command in string format. This function creates a pope between the calling application and the command which is executed. Popen() function returns the pointer to the stream which is used to read and write to the pipe created by the Popen() function. This function does not wait for the program to be executed.

Below is an example code and output.

CODE :

# importing subprocess module
import subprocess

# storing output of Popen() function.
output = subprocess.Popen(
                        'mkdir subprocess',
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT,
                        shell=True)

# Using communicate() function extracting
# error and output of Popen function()
stdout,stderr = output.communicate()

print(stdout)
print(stderr)

# Print the directories in the working directories
subprocess.call('ls',shell=True)

OUTPUT :

b''
None
test1.md  test2.md  test3.md  test4.md  subprocess  temp.py

In the above code and example you can see by using two functions of subprocess module you can run Bash commands in Python programming language. The Popen() function returns an object which has output and error of the Popen() function in a tuple. The communicate() method can be used to extract output and tuple. As you can see in output there are no errors hence the stderr has returned None.

Summary

In this Python article, How to run Bash commands in Python, we learned about bash and some most used commands of bash. Also we learned about functions and methods through which you can run Bash commands in Python. os.system() and subprocess.call() are for smaller tasks of bash, but the Popen() function of subprocess module and popen() of os module can be used for advanced bash works with output and inputs to other commands because these methods create pipes for I/O. You can always use any of the methods above according to your need.

Make sure to read and write example codes, in order to have a better understanding of this problem. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top