Find the answer to your Linux question:
Results 1 to 3 of 3
Hi Im wondering how to compare floating point variables. I tried: Code: var1=1.1111 var2=1.0001 if [ $var1 -gt $var2 ] then X=1 else X=0 fi But bash thinks that the ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    9

    Compare Floating Point Variables

    Hi Im wondering how to compare floating point variables. I tried:

    Code:
    var1=1.1111
    var2=1.0001
    if [ $var1 -gt $var2 ]
    then X=1
    else X=0
    fi
    But bash thinks that the numbers are equal. Any ideas?

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    I think you'll need to use bc.

    Floating Point Math in Bash | Linux Journal

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by birdman View Post
    I think you'll need to use bc.

    Floating Point Math in Bash | Linux Journal
    Note that at the end of the comments after the article shells ksh and zsh are mentioned. They have such arithmetic builtin. If you are concerned about portability to bash and other Bourne shell family members or size (of your system), then bc is probably best. Notice also that other scripting languages are mentioned: if you have the time, space, knowledge, and a lot of arithmetic to do, e.g. awk, perl, etc.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...