0

I want to dive integers but my results would be a digit bash can't do digits:

from=69;to=64;steps=$(( 12-1 )); ssize=$(( (to - from) / steps ))
echo $ssize $steps

results

0 11

Edit:

bc works with multiplication:

echo "5.94*11.14" | bc

gives 66.17

but division

echo "5.94/11.14" | bc

gives 0

1 Answers1

1

As was already pointed out, bash cannot handle floating point calculation by itself. You're doing a floor-division

(69-64)//11 = 5//11 = 0

Instead use bc

steps=12;ssize=$(bc -l <<< "from=69;to=64;(to - from)/$steps;"); echo $ssize $steps

The option -l will load the mathlib and with it set scale=20 telling bc how many digits after the period to handle. The default is 0, that's why you're also seeing only integer division when invoking bc without -l. Alternatively set scale manually

steps=12;ssize=$(bc <<< "scale=20;from=69;to=64;(to - from)/$steps;"); echo $ssize $steps
Nephente
  • 5,595
  • 1
  • 17
  • 23