DEV Community

Cover image for Running Bash commands with Python
Mikołaj Buchwald
Mikołaj Buchwald

Posted on

Running Bash commands with Python

When plain Bash is not enough

A while ago I read an excellent introductory Bash tutorial on Dev.to by Sahil Bondre.

It is a really great tutorial on shell scripting basics, but I remember when I was beginning my adventure with shell scripting, and when I was (over)fascinated with Bash. In time, when my neuroimaging scripts started becoming more and more complex, I slowly began loosing control over my code!

Many times I felt like that:

Linux bash why won't you work - crying peter parker | Meme Generator

The Python era

That was about that time when I discovered, that at some point, when more complicated logical expressions were needed (more conditionals, loops, and functions), it’s worth switching from Bash to Python. Hopefully, with Python you can wrap (almost) any bash command. And it is as simple as that:

import subprocess as sp

bash_command = "ls ~/"
print("\nBash command:\n%s\n" % bash_command)

process = sp.Popen(bash_command, stdout=sp.PIPE, shell=True)
output, error = process.communicate()

print("\nBash command output:\n%s\n" % output)
print("Bash command error:\n%s\n" % error)
Enter fullscreen mode Exit fullscreen mode

(It works both for Linux and macOS.)

Note the additional prints, as it is easier to debug the command before it is run. Additionally, you can see the output of the command (just like for a normal bash shell).

The above example was inspired by this StackOverflow answer.

It turned out that Python syntax is more readable and easier to maintain, yay! 🙂

How To Write Better Python Code? - DEV Python is just a better “pseudocode compiler” than Bash.

Take ~/ message

Don't stick with a plain bash for too long — often all you really needed is Python! Nevertheless, for a very simple scripts, you can of course always make a use of a good ol’ Bash ;)


See also my original post from 2020 (now it is already 2023 :) , but the knowledge is still a very much up-to-date): https://www.mindyourdata.org/posts/when-plain-bash-is-not-enough/


Cover photo source: Photo by Abhishek rana on Unsplash

Top comments (0)