The other day I wanted to perform some calculations via bash, but ended up getting an error “invalid arithmetic operator”. Here’s what you can do.


Bash and floating point arithmetic

Bash does not support floating point calculations. In order to calculate decimal numbers we are going to use the command bc. Bc stands for bench calculator or basic calculator and is the go-to language for calculating stuff when you’re on a Linux distribution such as CentOS.

Whenever you try to do a floating point calculation in bash you will get the error:

invalid arithmetic operator (error token is ...)

Installing BC

By default BC is not installed on CentOS, so we are going to install it

yum install bc

If you get the error PYCURL ERROR 22, you find the fix here

Installing Bc On Centos 6 5.Png

That’s it – bc is now installed and you should be good to go.