bc command acts like a calculator.
- It is usually used to set variables, using echo, pipe
- Use the
-l option to get accurate floating-point arithmetic.
$ a=`echo "3 * $a + 1" | bc -l`;
$ echo $a
- Note: the double quotes stop the
* being used as a wildmask for filenames.
- Numerical comparisons of non-integers can be done with
bc where 0 is returned for false and 1 for true.
echo "-1.2 < 0.5" | bc gives 1
echo "-1.2 > 0.5" | bc gives 0
So in an if statement do something like:
if [ `echo "$a < $b" | bc -l` = 1 ] ; then ...