Check whether one number equals another number in Bash

It must be:

if [ $x -eq 3 ]; then .....

If you prefer a more readable and self-explanatory code, use this syntax:

if test $x -eq 3; then .....

Explanation:

To compare integers you must use those operators (copied from man test):

INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
    INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
    INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
    INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
    INTEGER1 is less than INTEGER2

INTEGER1 -ne INTEGER2
    INTEGER1 is not equal to INTEGER2

operators == and != are for string comparison only.

For information: “[” command is an alias for system “test” command.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)