
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: February 25, 2024
In this tutorial, we’ll learn how to compare floating numbers in Bash.
As Bash doesn’t have floating-point arithmetic built-in, it treats numbers with decimal points as strings. Therefore, we’ll be looking at tools that add this functionality to Bash.
First, we’ll focus on using the command-line calculator, such as bc. Then, we’ll learn how to do a comparison using the awk command. Finally, we’ll look at the sort command to achieve it.
Before diving into the solutions, let’s check why we can’t use native Bash syntax for floating numbers comparison.
We’ll look at the following command that compares two floating numbers. It should print “OK” using the echo command if the comparison is correct:
$ (( 10.3 < 12.3 )) && echo "OK"
bash: ((: 10.3 < 12.3 : syntax error: invalid arithmetic operator (error token is ".3 < 12.3 ")
The command uses double parenthesis to compare the numbers. Then, the && operator checks if the comparison worked out well, in which case it should print the “OK” message.
As we can see, the “OK” message hasn’t been printed. Instead, Bash has thrown a syntax error because it doesn’t have built-in support for floating-point numbers.
We can use the bc command to compare floating-point numbers in Bash. bc is a command-line calculator application with a wide support of mathematical operations.
To compare two floating numbers, we use piping from the echo command:
$ echo "10.3 < 12.3" | bc
1
Here, we get the output 1 because the floating number 10.3 is indeed less than 12.3.
If the condition wasn’t correct, then the output would be 0.
If we want to integrate the above command into a Bash script conditional statement, we can use the following command:
$ if [ 1 -eq "$(echo "10.3 < 12.3" | bc)" ]; then echo "OK"; fi
OK
Let’s take a closer look at this command:
As we can see, the “OK” message has been printed correctly.
For simple calculations on floating point numbers, we can use the awk command:
$ echo 12.3 10.3 | awk '{if ($1 < $2) print $1; else print $2}'
10.3
This command compares the numbers 12.3 and 10.3 and prints out the smaller number, which is 10.3 in our case.
The command uses the following syntax:
We should note that the awk language wasn’t designed for complex math operations, so it has certain limitations. For example, if we need to do a high-precision number comparison, it’s preferable to use the bc calculator instead.
We can use the sort command with the -g option to compare floating numbers.
For example, let’s use sort to get the minimum number:
$ printf '12.4\n10.3\n' | sort -g | head -1
10.3
Let’s look at this command in more detail:
As we can see, the command correctly outputs 10.3, which is a smaller number than 12.4.
In this article, we learned how to compare floating point numbers in Bash. First, we analyzed why Bash doesn’t accept floating point numbers. Then, we looked at the bc, awk, and sort commands to get the floating number compared correctly.