FP comparison in Shell

People tend to not like Shell. But I do!
Here is a simple example, try this floating point comparison:

$ [ 0.1 -gt 0.01 ]
[: 0.1: bad number

The shell itself cannot use float.
But there are multiple workarounds. Here is the one I prefer:

if rpnc "$a" "$b" - | grep "^-" > /dev/null
  echo "a < b"

Although you may not have the rpnc command, so here is another one:

if [ $(echo "$a < $b" | bc) -eq 1 ]
  echo "a < b"

Leave a Reply

Your email address will not be published.

Captcha (enter your response as a number and not as a word, for instance 42 instead of forty-two) *
fifty three − forty seven =