Numeric comparison

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Conditional expressionHomeString comparison →

The test command can perform various numeric comparison using the following operators:

Operator Syntax Description Example
eq INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
#!/bin/bash
read -p "Please enter and confirm number 10 via keyboard : " n
if test $n -eq 10
then
    echo "Thanks for entering 10 number."
fi

 
ge INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
#!/bin/bash
read -p "Enter number >= 10 : " n
if test $n -ge 10
then
    echo "$n is greater than or equal to 10"
fi

 
gt INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
#!/bin/bash
read -p "Enter number > 20 : " n
if test $n -gt 20
then
    echo "$n is greater than 20."
fi

 
le INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
#!/bin/bash
read -p "Enter backup level : " n
if test $n -le 6
then
    echo "Incremental backup requested."
fi

if test $n -eq 7
then
    echo "Full backup requested."
fi

 
lt INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
#!/bin/bash
read -p "Do not enter negative number here : " n
if test $n -lt 0
then
    echo "Dam! you entered negative number!!"
fi

 
ne INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
#!/bin/bash
read -p "Do not enter -1 number here : " n
if test $n -ne -1
then
    echo "Thanks for not entering -1."
fi

 
← Conditional expressionHomeString comparison →