1. Overview

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.

2. Error When Using Native Syntax

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.

3. Using the bc Command

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:

  • The expression if …; then …; fi is for Bash script if statements.
  • The square brackets are used to perform string and arithmetic comparisons in Bash.
  • 1 -eq “$(…)” means to check if 1 is equal to the output of the following expression. In our case, the output of the “$(…)” expression is 1 if the comparison is correct.
  • The echo “OK” command is to print the “OK” message if our comparison worked out well.

As we can see, the “OK” message has been printed correctly.

4. Using the awk Command

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:

  • echo <num1> <num2> provides the two floating point numbers for the comparison.
  • The above is piped into the awk command using the piping symbol |.
  • The awk command uses the if statement, where $1 stands for the first number from the echo command, and $2 is the second one.

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.

5. Using the sort Command

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:

  • The printf command provides the numbers for a comparison. The numbers are given via the new-line character \n, as the new lines are needed for the correct sorting procedure.
  • sort -g means to use the general numeric sort
  • head -1 is to print the first line of the output of the sort command. In our case, the first line is the minimum floating point number.

As we can see, the command correctly outputs 10.3, which is a smaller number than 12.4.

6. Conclusion

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.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.