How can I do float comparison in Bash? How can I do float comparison in Bash? bash bash

How can I do float comparison in Bash?


Bash itself can't use float. In this case maybe you can multiply by 10 or 100 (etc.) and get integer value which you can compare. Or, you can use bc comparison and return value:

echo "10.2>10.1" | bc


# float number comparisonfcomp() {    awk -v n1="$1" -v n2="$2" 'BEGIN {if (n1+0<n2+0) exit 0; exit 1}'}# test and examplefcomp_test() {    if fcomp "$1" "$2"; then       echo "$1<$2"    else       echo "$1>=$2"    fi}fcomp_test 0.0 0.1fcomp_test 0.1 0.1fcomp_test -0.1 0.1fcomp_test 1e3 1e4fcomp_test -1.34e3 1.03e4fcomp_test ' 0 ' ' 1 '