When do you move from Bash to Python?
When I realize the need to automate a process, or the need to create something in a consistent manner, my brain first turns to bash. Bash is very easy to work with, and is remarkably consistent across linux systems. Bash comes pre-installed on every major linux operating system, and most linux users are familiar enough with bash, that they will be able to follow your script and know what it is doing, and modifications if necessary. This is a big deal when you work on a team. You want your work to be able to be readable, and for other people to understand what you are doing, and why you are doing it. Adding comments in your scripts will help, but writing readable code can be just as important.
There are times, when writing bash scripts, where I start to question whether bash is the right tool for the job. Personally, I draw the line when I reach the following qualifiers:
Working with Arrays:
If the dataset that you are working with comes to you in an array, or it makes more sense to put your data in an array, I immediately look to move away from bash.
Instantiating an array in Bash
And the same array and loop in python:
As you may have guessed by the examples above, my first, and basically only choice of programming language to use instead of bash, is python. Python is the most widely used language other than bash among sys-admins (at least in my experience). Also, even if you aren’t familiar with python, it has a very clean syntax, and it makes for very readable scripts. Also, python is essential to the linux operating systems, and while there are still issues between python2 and python3 consistency, for more simple scripts, the differences are minor and workarounds can be easily made to accommodate older python versions.
Bash does not keep track of what type your variables are, and this can be very frustrating when you have to do a lot of math. Python on the other hand, is a joy to work with when doing more complex math. It is easier to debug what type of variable you are working with in python, and you will get an error when the type of variables do not match.
What would happen if I tried to add a number to a string:
First in Bash:
What is even going on with bash math???
Now in python:
Thank you python, don’t let me do dumb stuff.
You want these errors to happen, otherwise you will get inconsistent or irrelevant results from your scripts.
Thousand Line Scripts:
Now, a thousand lines is arbitrary, but you know when a script is getting to big for its own good. I am a big advocate of using functions in bash <link to bash functions article>. But even with well managed bash functions, it can be difficult to keep track of the workflow of an extremely long bash script.
Python does a much better job of workflow in, if you follow a few best practices. Also, python has the ability to import modules that provide a specific functionality (and yes, I know that bash can import other bash scripts, much in the same manner as python, but I have NEVER seen anyone effectively use this feature, particularly in a production environment).
Passing parameters to a bash function:
And in python:
This is a great article about bash scripting as well: