How do I use floating-point division in bash?

Medya Gh picture Medya Gh · Oct 4, 2012 · Viewed 333.9k times · Source

I am trying to divide two image widths in a Bash script, but bash gives me 0 as the result:

RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))

I did study the Bash guide and I know I should use bc, in all examples in internet they use bc. In echo I tried to put the same thing in my SCALE but it didn't work.

Here is the example I found in the tutorials:

echo "scale=2; ${userinput}" | bc 

How can I get Bash to give me a float like 0.5?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Oct 4, 2012

You can't. bash only does integers; you must delegate to a tool such as bc.