floating-point operations with bash

quickbug picture quickbug · Dec 21, 2013 · Viewed 13.6k times · Source

how can I transform the string "620/100" into "6.2" in a bash script

The context of my question is about image processing. EXIF data are coding the focal length in fractional format, while I need the corresponding decimal string.

Thanks for helping, Olivier

Answer

anubhava picture anubhava · Dec 21, 2013

Use bc -l

bc -l <<< "scale=2; 620/100"
6.20

OR awk:

awk 'BEGIN{printf "%.2f\n", (620/100)}'
6.20