Bash Shell Number Comparison

www.‮l‬autturi.com
Bash Shell Number Comparison

In the bash shell, you can use the test command or the [ ] syntax to compare numbers and perform actions based on the comparison results.

Here are some examples of how to compare numbers in the bash shell:

  1. Equality: To test if two numbers are equal, you can use the -eq operator. For example:
if [ $num1 -eq $num2 ]
then
    echo "Numbers are equal"
fi

This will execute the echo command if num1 is equal to num2.

  1. Inequality: To test if two numbers are not equal, you can use the -ne operator. For example:
if [ $num1 -ne $num2 ]
then
    echo "Numbers are not equal"
fi

This will execute the echo command if num1 is not equal to num2.

  1. Greater than: To test if one number is greater than another, you can use the -gt operator. For example:
if [ $num1 -gt $num2 ]
then
    echo "Number 1 is greater than number 2"
fi

This will execute the echo command if num1 is greater than num2.

  1. Less than: To test if one number is less than another, you can use the -lt operator. For example:
if [ $num1 -lt $num2 ]
then
    echo "Number 1 is less than number 2"
fi

This will execute the echo command if num1 is less than num2.

  1. Greater than or equal to: To test if one number is greater than or equal to another, you can use the -ge operator. For example:
if [ $num1 -ge $num2 ]
then
    echo "Number 1 is greater than or equal to number 2"
fi

This will execute the echo command if num1 is greater than or equal to num2.

  1. Less than or equal to: To test if one number is less than or equal to another, you can use the -le operator.
Created Time:2017-10-16 14:38:38  Author:lautturi