Compare floating point number in Bash

by Daniel Pham
Published: Updated:

This article will show you how to compare floating point number in bash.

Here are my shared experiences while working with the Linux system.

Compare 2 common numbers

Usually, we compare two integer numbers. For example, we have 2 numbers as follows:


And to compare these two numbers in Linux, we can type the command below.

if [[ "$NUM1" -ge "$NUM2" ]]; then echo "TRUE"; else echo "FALSE";fi

Everything seems to be fine until now. But if we change NUM1=0.5 for example. You will get an error below.

bash: [[: 0.5: syntax error: invalid arithmetic operator (error token is ".5")

bc command

In linux, we have a command that allows performing calculations that is bc command.

Recommended Reading: Display program’s info entry with info command

According to man page, bc is:

bc is a language that supports arbitrary precision numbers with interactive execution of statements

Notice the -l option of the bc command, which is equivalent to --mathlib, which will load the standard math library.

Compare floating point number

Basically, we will have two cases to compare as follows.

compare floating point number in bash
Compare floating point number in Bash.

Floating point number with integer number

Recommended Reading: Calculate percent of memory used in Linux

For example, we have 2 numbers as follows:


We can see NUM2 is larger than NUM1. And if the comparison expression is [ NUM1 larger than NUM2 ], the expected result will be FALSE.

Run the command below.

if (( $(echo "$NUM1 > $NUM2" | bc -l) )); then echo "TRUE"; else echo "FALSE";fi

Compare two floating point numbers

Now, we will compare floating point number with other floating point number.

With the same comparison command above, we will now change the value for two numbers NUM1 and NUM2.

We set the value as below.


We can see, the number NUM1 is greater than NUM2. So the result we expect will be TRUE.

if (( $(echo "$NUM1 > $NUM2" | bc -l) )); then echo "TRUE"; else echo "FALSE";fi


I have written scripts for various purposes. And in those times, many times I have encountered the need to compare two values that are two floating-point numbers.

Without the bc command and not knowing how to use it, this will become very difficult. Hope this small article makes it easier for you to work.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.