Bash Shell Number Comparison

by on January 29, 2013 · 0 comments· LAST UPDATED January 29, 2014

in , ,

I am new Unix/Linux user. I am learning shell scripting. How can I compare numbers in bash shell?

You need to use the test command to perform various numeric comparison using the following operators:

  1. INTEGER1 -eq INTEGER2 - INTEGER1 is equal to INTEGER2
  2. Tutorial details
    DifficultyEasy (rss)
    Root privilegesNo
    RequirementsNone
    Estimated completion time1m
  3. INTEGER1 -ge INTEGER2 - INTEGER1 is greater than or equal to INTEGER2
  4. INTEGER1 -gt INTEGER2 - INTEGER1 is greater than INTEGER2
  5. INTEGER1 -le INTEGER2 - INTEGER1 is less than or equal to INTEGER2
  6. INTEGER1 -lt INTEGER2 - INTEGER1 is less than INTEGER2
  7. INTEGER1 -ne INTEGER2 - INTEGER1 is not equal to INTEGER2

Say hello to test utility

The test command evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). The syntax is:

test condition
test condition && do_something || do_nothing_due_to_false
[ condition ] && do_something || do_nothing_due_to_false

The syntax with if command is as follows:

 
if [ condition ]; then
       do_run_this_due_to_true_condition
else
       do_run_this_due_to_false_condition
fi
 

Example: Comparing numbers in bash

Find out if 5 greater than 10, enter (type command at terminal):

 
x=5
y=10
[ $x -gt $y ]
echo $?
 

Sample outputs:

1

In a bash shell non-zero output means false result i.e. $x is not greater than $y. Try the following example (type command at terminal):

 
x=51
y=10
[ $x -gt $y ]
echo $?
 
0

A zero value means true result i.e $x is greater than $y. Let us make output more readable using the following syntax:

 
[ condition ] &&  true-command || false-command
 

Find out if 5 greater than 10, enter (type command at terminal):

 
x=5
y=10
[ $x -gt $y ]  && echo "Yes \$x > \$y " || echo "No \$x is not > \$y"
 
No $x is not > $y

You can use if statement as follows:

#!/bin/bash
read -p "Enter a number (must be greater than 20) : " n
if test $n -gt 20
then
    echo "$n is greater than 20."
else
    echo "You are not following my instructions."
fi
 

OR

#!/bin/bash
read -p "Enter a number (must be greater than 20) : " n
if [ $n -gt 20 ]; then
    echo "$n is greater than 20."
else
    echo "You are not following my instructions."
fi
 

Run it as follows:

./script.sh
Enter a number (must be greater than 20) : 22
22 is greater than 20.
./script.sh
Enter a number (must be greater than 20) : 8
You are not following my instructions.

Arithmetic tests options

You can see a list of all supported options it by typing the following command:
$ help test
Sample outputs:

    File operators:
 
        -a FILE        True if file exists.
        -b FILE        True if file is block special.
        -c FILE        True if file is character special.
        -d FILE        True if file is a directory.
        -e FILE        True if file exists.
        -f FILE        True if file exists and is a regular file.
        -g FILE        True if file is set-group-id.
        -h FILE        True if file is a symbolic link.
        -L FILE        True if file is a symbolic link.
        -k FILE        True if file has its `sticky' bit set.
        -p FILE        True if file is a named pipe.
        -r FILE        True if file is readable by you.
        -s FILE        True if file exists and is not empty.
        -S FILE        True if file is a socket.
        -t FD          True if FD is opened on a terminal.
        -u FILE        True if the file is set-user-id.
        -w FILE        True if the file is writable by you.
        -x FILE        True if the file is executable by you.
        -O FILE        True if the file is effectively owned by you.
        -G FILE        True if the file is effectively owned by your group.
        -N FILE        True if the file has been modified since it was last read.
 
      FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                       modification date).
 
      FILE1 -ot FILE2  True if file1 is older than file2.
 
      FILE1 -ef FILE2  True if file1 is a hard link to file2.
 
    String operators:
 
        -z STRING      True if string is empty.
 
        -n STRING
        STRING         True if string is not empty.
 
        STRING1 = STRING2
                       True if the strings are equal.
        STRING1 != STRING2
                       True if the strings are not equal.
        STRING1 < STRING2
                       True if STRING1 sorts before STRING2 lexicographically.
        STRING1 > STRING2
                       True if STRING1 sorts after STRING2 lexicographically.
 
    Other operators:
 
        -o OPTION      True if the shell option OPTION is enabled.
        ! EXPR         True if expr is false.
        EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
        EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
 
        arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                       -lt, -le, -gt, or -ge.
 
    Arithmetic binary operators return true if ARG1 is equal, not-equal,
    less-than, less-than-or-equal, greater-than, or greater-than-or-equal
    than ARG2.
See also
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: