Linux shell/bash compare decimal numbers

 

Comparing decimal numbers with bash/shell script is bit tricky operation

I use following script to compare decimal numbers

#!/bin/bash
NUM1=2.0
NUM2=3.0
if (( $(echo "$NUM1 < $NUM2" | bc -l) ));
then
  echo $NUM1 is less than $NUM2
else
  echo $NUM2 is less than $NUM1
fi

I have written compare.sh file. To execute above script use bash instead of shell as shown below:

$ bash compare.sh 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s