pythonbashdc

bash + how to verify if number ( float or integer ) is less then integer number 1


my task is to verify if $VAL NUMBER could be float or integer , is less then number 1

I did

val=0.999
[[ $val -lt 1 ]] && echo less then 1
-bash: [[: 0.999: syntax error: invalid arithmetic operator (error token is ".999")

what is the right way to compare any $val number ( float or integer ) and test it if the value is less than 1?

solutions can be also with Perl/Python line linear that will be part of my bash script


Solution

  • Use bc. Write the expression you want to evaluate to bc's standard input, and it will output the result. In this case, a boolean expression will produce 0 if false, 1 if true.

    if [[ $(echo "$val < 1" | bc) == 1 ]]; then
        echo less than 1
    fi