Comparing Numbers in Bash Scripting

How can I compare numbers in Bash?

In Bash, you should do your check in an arithmetic context:

if (( a > b )); then
...
fi

For POSIX shells that don't support (()), you can use -lt and -gt.

if [ "$a" -gt "$b" ]; then
...
fi

You can get a full list of comparison operators with help test or man test.

Why is [[ 10 2 ]] true when comparing numbers in bash?

Because you compare strings according to Lexicographical order and not numbers

You may use [[ 10 -lt 2 ]] and [[ 20 -lt 2 ]]. -lt stands for Less than (<). For Greater than (>) -gt notation can be used instead.

In bash double parenthesis can be used as well for performing numeric comparison:

if ((10 < 2)); then echo "yes"; else echo "no"; fi

The above example will echo no

Comparing numbers using Bash

-le and -ge are like <= and >=.

  • -le = less than or equal
  • -ge = greater than or equal
  • -lt = less than
  • -gt = greater than

It'll work if you switch to -lt and -gt:

if [[ $X -lt $Y ]]
then
echo "X is less than Y"

elif [[ $X -gt $Y ]]
then
echo "X is greater than Y"

else
echo "X is equal to Y"
fi

You can also use (( )) for arithmetic operations. It has more natural syntax: you can use < and >, you don't need spaces around the operators, and $ dollar signs are optional.

if ((X < Y))
then
echo "X is less than Y"

elif ((X > Y))
then
echo "X is greater than Y"

else
echo "X is equal to Y"
fi

Compare numbers in shell

Wrong operator. Correct way of writing is:

if (( response1 > 400 || response1 < 200 || response2 > 400 || response2 < 200 )) ; then

No need to explicitly dereference with $, als long as it is ensured that your variables contain just integer numbers.

Compare Numbers not working properly (Bash Script in Hacker Rank)

I'm not sure why you're getting that result; I get "X is less than Y" in actual bash. However, your script is actually wrong in a different way: in [[ ]], < and > do alphabetic comparison rather than numeric comparison. To understand the difference, consider that [[ 5 < 1000 ]] will come out as false, because "5" comes after "1" in character sorting order. To do numeric comparison, use -lt and -gt instead.

Shell equality operators (=, ==, -eq)

= and == are for string comparisons

-eq is for numeric comparisons

-eq is in the same family as -lt, -le, -gt, -ge, and -ne

== is specific to bash (not present in sh (Bourne shell), ...). Using POSIX = is preferred for compatibility. In bash the two are equivalent, and in sh = is the only one that will work.

$ a=foo
$ [ "$a" = foo ]; echo "$?" # POSIX sh
0
$ [ "$a" == foo ]; echo "$?" # bash-specific
0
$ [ "$a" -eq foo ]; echo "$?" # wrong
-bash: [: foo: integer expression expected
2

(Note: make sure to quote the variable expansions. Do not leave out the double-quotes above.)

If you're writing a #!/bin/bash script then I recommend using [[ instead. The double square-brackets [[...]] form has more features, a more natural syntax, and fewer gotchas that will trip you up. For example, double quotes are no longer required around $a:

$ [[ $a == foo ]]; echo "$?"      # bash-specific
0

See also:

  • What's the difference between [ and [[ in Bash?

Comparing an integer variable in the Bash shell

This works - the $ in front of the i in the new_i=$(( line and the removal of the quotes and one set of the brackets fixed the errors and now the scripts works as intended. Thanks to everyone for their help!

#!/bin/sh
i=$(cat /etc/hour.conf)
new_i=$(($i+1))
if [ $new_i -gt 15 ]; then
new_i=0
fi
echo "$new_i">/etc/hour.conf
echo "$new_i"


Related Topics



Leave a reply



Submit