Share via:

Why Are We Still Using Bash?

Article trying to explain why you may not want to use bash for larger scripting work.

23 views

Edited: 2019-10-04 15:34

The process of learning bash has been extremely painful for me. I have probably spent several days (and some nights) learning the exact strict syntax. But, before this, I have done various smaller .sh scripts when I needed them.

Recently I made my largest bash script to date, which is trying to fix a problem occurring on resolution change in KDE Plasma. I however had to use PHP to do a simple literal string replacement, since it was just too difficult using commands in bash alone. I will try again later, when I get the time.

Bash scripting is probably one of the hardest things I have learned so far, and there is still things I have not found out how to do yet.

Everything is easy with practice, or so they say. But, there is also the issue of other people reading your code. The question is, why are we still doing scripting in bash when we have better, easier, languages such as PHP, JavaScript or Python?

Why is bash so hard?

Part of the reason, I think, is you are not really using bash to do much anyway, since you are essentially just calling system programs (commands) while hoping that they are installed on the system. What you should be doing, is first check if each command you use in the script is installed before you use them. This is simple reasonable efforts towards writing code that is more reusable and portable. Which brings me to the next issue.

Bash is not object orientated. You could probably code-around this, and introduce your own OOP functionality. But, why would you? It does have functions, however:

my_function () {
  echo "hallo world"
}

# Calling my_function()
my_function

Variables are global, and while this makes them easy to understand, it also means that even if you declare a variable inside a function, it will be available globally.

Also, when declaring your variables, you can not have spaces before or after the equals sign (=).

And likewise, the syntax of if statements is not completely straight forward to someone coming from another language, and takes some getting-used-to. The problem I have with the syntax is, it has to be very precise. You have to use square brackets [ $something = $otherthing ], and even if just a space at the start or end is missing, you will get an obscure error message.

Script example:

something="bah"
otherthing="bah"

if [$something = $otherthing ]
then
    echo "yeah"
fi

The above will result in an error like this:

./my_script.sh: 8: ./my_script.sh: [bah: not found

You have to be very precise, and include a space at both the start and the end:

something="bah"
otherthing="bah"

if [ $something = $otherthing ]
then
    echo "yeah"
fi

Quoting and escaping nightmare

In bash, there are just so many "special cases" that it can be very hard to write portable scripts.

Dealing with spaces in path names is one example, but that is just a minor problem that I had to deal with. I solved it by doing this, when calling another script:

echo "$(php "$script_dir"/str_replace.php "$source" "$replacement")"

Note. the use of double quotes.

As shown above, spaces in path names can be dealt with relatively easy by quoting the variable containing the path. Escaping spaces is not necessary afaik. But, my point is that such hackery should not be needed in the first place.

This brings me to my next point. Obtaining the path for the directory of the script is not as straight forward as I am used to in PHP. People have come up with complex multi-line functions to do this. I simply choose to do:

script_dir=$(dirname "$(readlink -f "$0")")
echo $script_dir

And this might not be the most portable way, but it works in my specific circumstances.

Conclusion

I think bash has a place when it comes to system administration, and for doing simple things. But, we have to be very careful, since there are so many "special cases" we have to deal with when writing scripts that are used by others.

I also think it is fine to use Python, PHP or even JavaScript, if you are more comfortable in those languages.

In addition to the complex syntax, there are also tiny differences between shells. Bash is not the same as dash, and sh does not always point to bash. For example, in more recent versions of Ubuntu, sh points to dash, which means your script might not work as intended unless great care is taken while writing the script.

Comments