BCE-O535 Linux and Shell Programming

0 of 30 lessons complete (0%)

Module 6 Introduction to Shell

Number Comparison

eqEqual to
-neNot equal to
-gtGreater Than
-geGreater than equal to
-ltLess than
-leLess 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"
fi

Not 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.