Although I've already shown you how to sum numbers up in bash, I only covered the bash way of doing it. I really like scripting with bash, but when it comes to calculations, there's quite a few important features missing from bash, and fixed point (thanks for the correction, Azrael Tod!) calculations is one of them. Fortunately, bc command comes as a standard in most Unix distros, and can be used for quite complex calculations.
Basic calculations with bc
bc is a very simple command. It takes standard input as an expression and then evaluates this, performing all the necessary calculations and showing you the result. Thus, to quickly sum numbers up or get a result of some other calculation, simply echo the expression and then pipe it out to the bc command:
ubuntu$ echo "1+2" | bc 3
Now, in scripts your calculations with bc are done quite similarly to what we did in bash. Here's an example:
ubuntu$ NUMBER1=1 ubuntu$ NUMBER2=2 ubuntu$ SUM=$(echo "$NUMBER1+$NUMBER2"| bc) ubuntu$ echo $SUM 3
I told you these calculations would be basic, right? Now onto the more interesting stuff!
Fixed point calculations with bc
Most people learn about bash math limitations when they attempt to do a simple calculation but can't get the current answer with fixed point values. By default, all the operations happen with integers, and that's what you would get:
ubuntu$ echo "1/2" | bc 0
Now, if you expect 0.5 to be the result of dividing 1 by 2, you need to explain it to bc, because by default it doesn't show you any fractional part of the number.
The way you do this is quite simple: all you have to do is specify the number of digits you'd like to see after the radix point of your result. For example, if I set this number to 5, I'll get bc to output the result of my calculation with 5 digits after the radix point. The special keyword to convey this intention to the bc command is called scale. Just specify the scale value and separate it from the rest of your expression by the semicolon sign:
ubuntu$ echo "scale=5; 1/2" | bc .50000
Here's another example:
ubuntu$ echo "scale=5; 0.16*10.79" | bc 1.7264
Hope this answers your question! bc command is very powerful, so I'll definitely have to revisit it again in the future. For now though, enjoy the fixed point calculations and be sure to ask questions if you think I can help!
- Summing numbers up in Unix scripts
- Basic math in Unix scripting
- Another way of doing calculations in scripts