Bash vs. Python: Which language should you use?

Both programming languages have pros and cons that make them better for some tasks than others.
197 readers like this.
Perl tricks for system administrators

Opensource.com

Bash and Python are most automation engineers' favorite programming languages. Both have pros and cons, and sometimes it can be hard to choose which one you should use. The honest answer is: It depends on the task, the scope, the context, and the complexity of the task.

Let's compare these two languages to get a better understanding of where each one shines.

Bash

  • Is a Linux/Unix shell command language
  • Is great for writing shell scripts that use command line interface (CLI) utilities, utilizing output from one command to another (piping), and executing simple tasks (up to 100 lines of code)
  • Can utilize command-line commands and utilities as-is
  • Has better startup time than Python but poor execution time performance
  • Does not come preinstalled in Windows; your script might not be compatible with multiple operating systems, but Bash is the default shell on most Linux/Unix systems
  • Is not fully compatible with other shells (e.g., csh, zsh, fish)
  • Piping ("|") CLI utilities like sed, awk, grep, etc. can slow its performance
  • Lacks many functions, objects, data structures, and multi-threading, which limits its use for complex scripting/programming
  • Lacks good debugging tools and utilities

Python

  • Is an object-oriented programming (OOP) language, so it's more general purpose than Bash
  • Can be used for almost any task
  • Works on most major operating systems and is also installed by default on most Unix/Linux systems
  • Is very similar to writing pseudo code
  • Has simple, clear, easy-to-learn, and easy-to-read syntax
  • Has lots of libraries, documentation, and an active community
  • Provides better error handling features than Bash
  • Has better debugging tools and utilities than Bash, which makes it a great language for developing complex software applications involving many lines of code
  • Applications (or scripts) can contain many third-party dependencies that must be installed before executing them
  • Requires writing more lines of code for simple tasks than Bash does

I hope these lists give you a better understanding of which language to use and when to use it.

Which language do you use more in your day-to-day work, Bash or Python? Please share in the comments.

Tags

16 Comments

Here's the math:
Bash has 3 nots, 2 lacks, 1 better, 1 poor, 1 great
Python has 2 betters, 1 great

This is not football. A "scoresheet" doesn't help when you're trying to pick a language for a task. First: look at your task. Second: pick the best language *for that task*. That's it. Sometimes, it will be Bash- scoresheet or no. I can tell you that 99.99% of those times it will be when provisioning or maintaining systems. Because bash is a domain specific language, not a general purpose programming language like Python. The confusion comes because Python all right in the system administration universe in many cases, but Bash's domain is exclusively there. Comparing the languages is apples-to-oranges, quite honestly.

In reply to by Greg P

It really depends on the task.
I love automating all things that talk with HTTP with Python.
Doing this in bash with curl is possible but ugly.

When interacting with other local programs, I like bash.

Bash is the native language of Linux system administration. You put things in bash shell scripts that you would otherwise be typing into a shell prompt.

Python is a programming language. You use it and other programming languages to do things that you would not otherwise accomplish by entering commands at a shell prompt.

Good info I love both but the fact that python can be used in almost any task tops it all.

Its not exactly apples to apples comparison, so it depends. I would prefer Bash for quick and small scripts over Python.

In reply to by Pyrax

Probably python, definitely for anything longer than few lines. There are also some nifty CLI tools (be it git or skopeo) which can be accessed from python (see subprocess.Popen(), shlex) if they don't have equivalent (Requests). On higher level Ansible comes to play... or Bash to complete the circle. :)

Yeah i prefer a blend of ansible/python/bash for many tasks, just makes my life easier :)

In reply to by Pavel Zubaty (not verified)

To the fact, bash is a lot slower than python. provided python was written in C and that makes it close to the the core of OS.

GNU Bash being native Linux tool was simply designed to perform regular administrative tasks, some of us does use it for a lot other things, which is good to some extent.

Bash is slower except for the startup time compare to Python, but anyway the difference is so tiny to find a human noticeable difference

In reply to by tamrakar

thanks . it's helpful

This is a nice resource Archit! :)

They complete each other. I write lots of small bash scripts on linux, like I do powershell on windows. I then run them through a modified subprocess and return response in json. All the ductape holding it together like auth, web, ssh, https etc is python.

Bash is also very os centric and relies heavily on linux commands. While it is quite easy to write python3 that work exactly the same on all windows/macOs/linux if you just pick modules that work all places. (most popular pypi modules do nowadays).

Do you have any data or proof that bash is slower? The overhead of setting up python, the fact that it breaks terribly between versions.. Its dependency hell and so many other cons are not mentioned. It's a rather biased opinion

As with most people it depends on the job. I tend to prefer bash if it's 10s of lines or less, especially if it's connecting several other utilities together to form one.

For bash, I disagree with one statement:
Piping ("|") CLI utilities like sed, awk, grep, etc. can slow its performance

It's the opposite... Piping CLI utilities can speed its performance as each step in the pipeline is parallelized and so it can be working on multiple portions at the same time, putting those multiple cores to use. This is very effective when doing several layers or processing through a TB file.

Python lacks support for true multithreading, although it can do similar to multiprocess as bash, but the | in bash makes it far easier than python for those tasks that lend tend themself to being run in a pipeline.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.