Bash Floating Number Variables Comparision


 
Thread Tools Search this Thread
# 1  
Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you.

Code:
#!/bin/bash

var1=100.25
var2=100.25

if (( $var1 == $var2 )); then
        echo "Matching"
else
        echo "Not Matching"
fi

Error:
Code:
./number.sh: line 6: ((: 100.25 == 100.25 : syntax error: invalid arithmetic operator (error token is ".25 == 100.25 ")
Not Matching

# 2  
Bash does not support floating point arithmetic. Use ksh93 instead, if possible.
# 3  
Interesting so there is no other way in bash shell to achieve this other than using korn shell ?
# 4  
There are many awkward ways to do so. For instance:

Code:
if awk -v VAR1="$VAR1" -v VAR2="$VAR2" 'BEGIN { exit(!(VAR1 == VAR2)); }'
then
...
fi

But ksh93 supports it directly, without external utilities.
This User Gave Thanks to Corona688 For This Post:
# 5  
Code:
export var1="100.25" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
match


export var1="100.55" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
no match

This User Gave Thanks to in2nix4life For This Post:
# 6  
Or you can use bc - An arbitrary precision calculator language:
Code:
if [ $( echo "$var1 == $var2" | bc -l ) -eq 1 ]
then
        echo "Matching"
else
        echo "Not Matching"
fi

This User Gave Thanks to Yoda For This Post:
# 7  
Quote:
Originally Posted by in2nix4life
Code:
export var1="100.25" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
match


export var1="100.55" && export var2="100.25"

[ $var1 == $var2 ] && echo "match" || echo "no match"
no match

This will not work when comparing 100.25 to 100.250
This User Gave Thanks to Corona688 For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Bash script accepting variables in valid number format

Hi Experts I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have ... (3 Replies)
Discussion started by: ersan-poguita
3 Replies

3. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

4. Shell Programming and Scripting

How to compare floating variables , integer value expected?

I am running some commands and I am trying to get an output into a variable. I am having problem when I try to put that value in while loop, it says integer value expected. What's the best way to accomplish this remaining=$(symclone -sid XXX -f Clone_test query | grep MB | awk '{print... (1 Reply)
Discussion started by: rajsan
1 Replies

5. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

6. Shell Programming and Scripting

Increment a floating number in ksh

Hi ! How to increment a varibale in ksh. #!/bin/ksh set -x RELEASE_NUM=5.2.103 VAL=0.0.1 RELEASE_NUM=`echo $RELEASE_NUM + $VAL | bc` echo $RELEASE_NUM The above code is throwing this error. + RELEASE_NUM=5.2.103 (2 Replies)
Discussion started by: dashok.83
2 Replies

7. Shell Programming and Scripting

floating point variables korn shell

Hi I'm not using Korn93 but want to use floating point variable. Is there any solution to do that ? thx for help. ---------- Post updated at 02:28 PM ---------- Previous update was at 12:38 PM ---------- I have the following peace of code: for n in `cat log.January.1.array` do ... (3 Replies)
Discussion started by: presul
3 Replies

8. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

9. Shell Programming and Scripting

HELP: compare floating point variables??

Hi All, I got this script that pulls the Amps value from our RPC's , I basiclly want to compare the valued with a "limit" value -- if the numbers match or are greater than the definded value ...do something. My problem is I cant seem to figure out how to compare floating points... here is... (1 Reply)
Discussion started by: zeekblack
1 Replies

10. UNIX for Dummies Questions & Answers

Number comparision in AWK

Hi, I have a file like this. "2006","10",25,"U","1129","32","C",0,0,0,0,0,0,0,0,0,0,0,0,352,16,4,0,0,0,0,0,"80",,1 "2006","11",25,"U","1148","32","C",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"80",,2 "2006","14",25,"U","1149","10","C",0,0,0,0,0,0,0,0,0,0,0,0,560,12,0,0,0,0,0,0,"80",,3... (1 Reply)
Discussion started by: vskr72
1 Replies

Featured Tech Videos