This tutorial explains comparing two numbers for equal or not in bash and shell programming.

Numbers can be integers or floating numbers.

How to Check if two numbers are equal or not in Bash

This program takes input values and checks if two values are the same or not.

first=13
second=15
if (( first == second )); then
  echo "Two numbers are equal";
fi

Some shell scripts do not support (()), use [[]] with Comparison operators

Following are Comparison operator.

  • -eq: equal

    • Check if two variables are equal
  • -ne : Not equal

    • Check if two variables are not equal
  • -lt : Less than

    • Check if first variable is less than second variable
  • -le : Less than equal

    • Check if first variable is less than equal to second variable
  • -gt : Greater than

    • Check, if first variable is greater than second variable
  • -ge: Greater than or equal

    • Compare Check if first variable is greater than equal to second variable

used the -eq operator in the if fi conditional statement

first=13
second=15

if [ "$first" -eq "$second" ]; then
  echo "Two numbers are equal";
fi

You can also do it with the ternary operator.

first=13
second=15

echo $(( first == second ?  "equal": "not equal" ))