Cogs and Levers A blog full of technical stuff

Doing math with bash

bash can be used to perform simple arithmatic when needed. It is only limited to integer mathematics though. You can see this by using the expr.

An excerpt about expr from the link above:

All-purpose expression evaluator: Concatenates and evaluates the arguments according to the operation given (arguments must be separated by spaces). Operations may be arithmetic, comparison, string, or logical.

Some trivial example usage shows that you can get some quick results if you only need integer math.

$ expr 1 + 1 
2
$ expr 1 - 7
-6
$ expr 6 \* 7
42
$ expr 6.5 \* 7
expr: non-integer argument

At this point, you could probably go for the full-nuclear option and get perl or python to perform floating point calculations; but to keep things a little more shell oriented, you can go with a lighter-weight option, bc.

bc is an arbitrary precision calculator language. Much like every other good shell tool, you can invoke it so that it’ll take its input from STDIN and return its output to STDOUT. Here are some example invocations:

$ echo "1+1" | bc
2
$ echo "1.9+1" | bc
2.9
$ echo "76/5" | bc
15
$ echo "scale=2; 76/5" | bc
15.20

You can see that if you want precision on your answers from integer inputs, you’ll need to set the scale variable to suit. Only feeding in static values is a bit basic though. To put this to work, you just need some variable data at hand.

What’s the percentage battery left on this notebook?

$ echo "scale=2;" $(cat /sys/class/power_supply/BAT0/charge_now) / $(cat /sys/class/power_supply/BAT0/charge_full) | bc
.30

Not much!