Basic Linux Tutorial
Computer Tutorial

Testing Numeric & String Values



Testing Numeric Values

The test command can be used to evaluate the numerical relationship between two integers. The return code of the test command will denote whether the condition was true (returning 0) or false (returning 1).

Synopsis :

[ number   relation   number ]

Example :



$ [ “$X” -lt 7 ] $ [ “$X” -gt 7 ]
$ echo $? $ echo $?
0   1

Relation Description
-eq Equality check
-ne Not equal
-lt Less than
-gt Greater than
-le Less than or equal to
-ge Greater than or equal to

Testing String Values

The test command can also be used to compare the equality or inequality of two strings. Other than that, a single string can be tested if it has a zero length or not.

Example :



$ X=abc $ X=abc
$ [ "$X" = "abc" ] $ [ "$X" != "abc" ]
$ echo $? $ echo $?
0   1

Relation Description
string1 = string2 True if string1 and string2 are equal
string1 != string2 True if string1 is not equal to string2
-z string True if string length is zero
-n string True if string length is nonzero
string True if string length is nonzero