• +43 660 1453541
  • contact@germaniumhq.com

Replacing Shell Scripts With Python I

Replacing Shell Scripts With Python I

I have a love hate relationship with Bash. On one hand, I have a lot of scripts still running on it. On the other hand, almost all of them were started before I consciously decided that for most things I’ll switch to Python.

Here are 5 problems that I see with Shell programming:

1. Variable Expansion Control

Let’s assume you have an expression such as:


If the TARGET_FOLDER happens to have spaces, or any character from $IFS actually, the variable that defines how to split output, you need to be careful on using that variable. A simple workaround is to use ":


2. IFS nightmare

Related to point 1, when doing chained operations, we need to be aware on the $IFS manually:

find . -type f -name \*.json | xargs grep "something"

This works well only if find doesn’t finds files that contain spaces. $IFS contains implicitly \t - tab, \n - enter, and _ - space.

One option is to manually change it, to only enters, but then we need to be aware to change it back after the operation since a broken $IFS can screw up other shell scripts.

3. for

Yes, this one has its own category. for iterates over lists separated by the $IFS. Want to iterate over lines in a file?

for i in `cat file.txt`; do
  echo $i

I lied. $IFS plays yet again a role, so you need to be careful what the program that the for uses outputs. So we’re creating a new process, capturing all the output just to have a basic iteration over the lines.

4. Numeric Operators

The numeric operators don’t follow any convention from other programming languages, for the simple reason that < and > are used to redirect inputs and outputs. Hence, to have an if that checks if a number is smaller than 10:

if [[ $i -lt 10 ]]; then
  echo "yay"
  echo "nay"

Yes, you need to use operators such as -lt, or -gt. Greater or equal is -ge. Different is -ne - not equal. I just searched in the manual, I can’t ever remember this.

5. Functions

Functions receive numeric parameters as if they are a regular script. Good look finding out what variables are supposed to mean. Having names it’s just so much more natural.

printout() {
    echo $1 $2

printout hello world

That’s why I’m using almost exclusively python now.