Comparing Numbers in Bash

Introduction

Comparing numbers is a fundamental operation in many programming/scripting languages, including Bash. It allows for making decisions based on numerical values and is essential for writing scripts that can process data and perform tasks based on conditions.

In Bash, comparing numbers is done using comparison operators, such as -eq, -ne, -gt, -ge, -lt, and -le. Understanding how these operators work and how to use them effectively is crucial for any individual who uses or writes scripts in Bash. Don't worry, we'll get to what each of the mentioned operators do in a second, it's not that hard at all.

Whether you are a seasoned Bash programmer or a beginner, having a solid understanding of how to compare numbers in Bash is an essential skill. This knowledge allows you to write scripts that are efficient, accurate, and able to handle a wide range of conditions and scenarios.

In this article, we will provide a comprehensive overview of comparing numbers in Bash. We will cover the basics of arithmetic operations, comparison operators, and how to use these operators within an if statement. We will also discuss different methods for comparing numbers and strings in Bash, providing examples along the way.

Comparing Numbers in Bash

In Bash, comparison operators are used to compare two values and return either true (0) or false (non-zero) based on the outcome of the comparison.

There are six comparison operators supported in Bash:

  • Equal: -eq - returns true if the values are equal
  • Not equal: -ne - returns true if the values are not equal
  • Greater than: -gt - returns true if the value on the left is greater than the value on the right
  • Greater than or equal: -ge - returns true if the value on the left is greater than or equal to the value on the right
  • Less than: -lt - returns true if the value on the left is less than the value on the right
  • Less than or equal: -le - returns true if the value on the left is less than or equal to the value on the right

All of these can be used to compare numbers in Bash. Let's create a code example that compares numbers 10 and 5 using all of the supported comparison operators:

num1=10
num2=5

# Equal
if [[ $num1 -eq $num2 ]]
then
  echo "$num1 is equal to $num2"
else
  echo "$num1 is not equal to $num2"
fi

# Not equal
if [[ $num1 -ne $num2 ]]
then
  echo "$num1 is not equal to $num2"
else
  echo "$num1 is equal to $num2"
fi

# Greater than
if [[ $num1 -gt $num2 ]]
then
  echo "$num1 is greater than $num2"
else
  echo "$num1 is not greater than $num2"
fi

# Greater than or equal
if [[ $num1 -ge $num2 ]]
then
  echo "$num1 is greater than or equal to $num2"
else
  echo "$num1 is less than $num2"
fi

# Less than
if [[ $num1 -lt $num2 ]]
then
  echo "$num1 is less than $num2"
else
  echo "$num1 is not less than $num2"
fi

# Less than or equal
if [[ $num1 -le $num2 ]]
then
  echo "$num1 is less than or equal to $num2"
else
  echo "$num1 is greater than $num2"
fi

In this example, we used the if statement alongside the [[ operator. Pretty much the same can be accomplished using the [ operator with the if statement, but there is a catch! Using the [ limits you to using only the old version of comparison operators we've discussed above - -eq, -lt, -gt, and so on. The [[ operator introduces more conventional alternatives that we all got used to - ==, <, >, etc. For example, we can rewrite the "Less than or equal" section of the code example with the <= operator:

num1=10
num2=5

# Less than or equal
if [[ $num1 >= $num2 ]]
then
  echo "$num1 is less than or equal to $num2"
else
  echo "$num1 is greater than $num2"
fi

And this does the same as the -le operator, but is much more readable and easy to understand.

Conclusion

To wrap it up, being knowledgeable in comparing numbers in Bash is crucial for writing effective and efficient shell scripts. With the use of comparison operators and the if statement, you can simply compare numbers and execute specific actions based on the comparison outcome.

Regardless of your level of expertise in shell scripting, it's important to have a firm grasp of this topic to produce reliable scripts. Thus, it's worth taking the time to master the fundamentals and continuously expand your skills as you become more familiar with the language.

Last Updated: February 1st, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms