# The Agony of Bash Math

Let’s take a look at a coding problem that has plagued me for months on end:

The accounts of the "Fat to Fit Club (FFC)" association are supervised by John as a volunteered accountant. The association is funded through financial donations from generous benefactors. John has a list of the first

`n`

donations:`[14, 30, 5, 7, 9, 11, 15]`

He wants to know how much the next benefactor should give to the association so that the average of the first`n + 1`

donations should reach an average of`30`

. After doing the math he found`149`

. He thinks that he could have made a mistake.if

`dons = [14, 30, 5, 7, 9, 11, 15]`

then`new_avg(dons, 30) --> 149`

Could you help him?

Wow that sounds really easy, it’s literally elementary algebra. Why would such a problem give me such difficulty?

Well, because I tried to use Bash scripting to solve it.

There are few things a budding developer will experience that will cause greater pain than trying to do even basic math with Bash. Of course, you can add, subtract, multiply and divide, provided you’re happy only ever dealing with integers. Want to use floating points? Calculate an average maybe? Well, I hope for your sake you’re some kind of masochist.

There’s one simple reason for Bash’s ineptitude at mathematics. Everything is a damn string. You end up relying on piping things to basic calculator or awk whenever you need to do anything more complicated than 1+3.

Even getting your script into a format Bash can understand is a torture unto itself, leading to hellish expressions such as:

`echo "$(echo "ibase=10;obase=2;$n" | bc | sed "s/0//g" | wc -c) - 1" | bc`

What the hell does that mean? I have no idea. Seriously, it’s piped to bash calculator *twice.* There’s no wonder something as simple as calculating basic algebraic expressions is such a miserable experience.

You’ll spend hours getting Bash to even understand your mathematical expressions, and then finally, it won’t crash. But then the work of debugging begins. After torturing yourself, you’ll give up and realise you should’ve just used Python all along.

There’s no doubt about it, mathematics in Bash is a recipe for misery.