| –eq | Equal to |
| -ne | Not equal to |
| -gt | Greater Than |
| -ge | Greater than equal to |
| -lt | Less than |
| -le | Less than equal to |
In Unix/Linux, you can perform numeric comparisons using conditional statements. These comparisons are essential for making decisions in shell scripts.
Here are some common comparison operators:
Equal to (-eq): Checks if two numbers are equal.
Example:
if [ $num1 -eq $num2 ]; then
echo "$num1 is equal to $num2"
fiNot Equal to (-ne): Checks if two numbers are not equal.
Example:
if [ $num1 -ne $num2 ]; then
echo "$num1 is not equal to $num2"
fi
Greater Than (-gt): Checks if the first number is greater than the second.
Example:
if [ $num1 -gt $num2]; then
echo "$num1 is greater than $num2"
fi
Greater Than or Equal to (-ge): Checks if the first number is greater than or equal to the second.
Example:
if [ $num1 -ge $num2]; then
echo "$num1 is greater than or equal to $num2"
fi
Less Than (-lt): Checks if the first number is less than the second.
Example:
if [ $num1 -lt $num2]; then
echo "$num1 is less than $num2"
fi
Less Than or Equal to (-le): Checks if the first number is less than or equal to the second.
Example:
if [ $num1 -le $num2]; then
echo "$num1 is less than or equal to $num2"
fi
Remember to replace $num1 and $num2 with actual numeric values or variables holding numeric values.
