Running simple bash script on terminal using Python

Python3




import os
 
os.system("echo w3wiki")


Output:

w3wiki

How to run bash script in Python?

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.

Similar Reads

Running simple bash script on terminal using Python

Python3 import os   os.system("echo GeeksForGeeks")...

Executing bash scripts using Python subprocess module

...

Execute an existing bash script 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....

Executing bash command using Python os module

...

Contact Us