Monthly Archives: December 2018

Maths in BASH

You know, this is one of those things where I had a complete WTF moment. It’s possible that EVERYONE except me knew about this, but I’ll mention it here in case this is as much a surprise to others as it was to me.

For decades, literally, I’ve been doing maths in BASH by doing

x=`expr $y + 3`

This is just how it was done, and how I was first shown how to do maths in BASH

A friend of mine chucked a script over to me a week or so ago, and I looked at it, and my brain almost exploded when I discovered that BASH has built-in maths functionality

x=$(($y + 3))

That simple.

I’d been seeing $(( )) in BASH for years, and I’d never been able to work out what double brackets were doing. Now, I’m wondering what else they might do, cos they’re going to save a whole ton of CPU for my scripts not needing to fire up an external process whenever I need to do 1+1

Yes, I feel slightly silly for not knowing this, but thought I’d mention it anyway.