• 0
Votes
name

The only way I could find out to compare floats in shell is:

A=12.3
B=12.2
if [ $(bc <<< "$B <= $A") -eq 1 ]

Direct comparison, as far as I know, doesn't happen.

but strangely the following code compares floats without bc:

A=13.7
B=13.2

[[ $A > $B ]] && echo "A is greater than B"

[[ $A < $B ]] && echo "A is less than B" 

This returns:

A is greater than B

As far as I know && executes the second command only and only if the first command returns with an exit status zero.

However, as suggested by Pixel Chemist in the comments if we attempt to use negative numbers in the second methods, it doesn't work and gives the opposite results.

Can someone please explain how is the second method working without bc.