Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-27-2012, 02:42 PM   #1
LQ Newbie
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
bash: how to compare REAL numeric values?


I would like to do an if conditional in bash to compare a numeric value, which is NOT an integer:

if [ "$VALUE" -eq 0.1 ]; then
... do something ...
... do something else ...

If $VALUE is a real variable (e.g., 0.5) and not an integer and I get an error message saying: $VALUE: integer expression expected.

How can I do this?
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-27-2012, 02:49 PM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
Bash does not handle floating point numbers. Use a real language (perl, python, C, etc) or a more modern shell (zsh) or insert calls to bc.

Last edited by AlucardZero; 02-27-2012 at 02:52 PM.
Old 02-27-2012, 02:54 PM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Native bash: you can't

Two options:
1. move your script to another language with real number support (e.g. python or perl). If you are dealing with floating point numbers, the task (in my experience) is better suited to one of those other languages than a shell script.
2. invoke an external command that can support real valued conditionals

For #2, I usually rely on the bc command.

For instance:
user@localhost$ echo " 3.5 < 9.1" | bc -l
user@localhost$ echo " 3.5 > 9.1" | bc -l
Use the above with command substitution. For example:
variableName=$( echo "${operand1} < ${operand2}" | bc -l )
And then test the value of variableName.

Alucard beat me to it!
2 members found this post helpful.
Old 02-28-2012, 09:00 AM   #4
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You can also do it in awk if bc is not available.

Originally Posted by AlucardZero View Post
Bash does not handle floating point numbers. Use a real language (perl, python, C, etc) or a more modern shell (zsh) or insert calls to bc.
So bash isn't a "real" language ?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 11:11 AM
[SOLVED] C Arguments to main - can't compare values madsovenielsen Programming 2 07-25-2011 08:17 AM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 04:36 PM
Compare Decimal Values rbautch Linux - General 10 04-22-2008 05:11 AM
Searching for numeric values in PHP TheBeli Programming 5 04-17-2008 04:55 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration