Testing Conditions

The shell provides a test command for testing conditions. The test commands test expressions to see whether the expression is true, as follows:

test $age -eq 21
[ $age -eq 21 ]

The two statements are equivalent. This chapter uses the second format. A space is required before and after each square bracket. The test returns true if $age equals 21.

Table 19-1 lists the options you can use to test values.

Table 19-1. Options for Use When Testing Expressions
OptionTests forExample
=equal text strings[ $name = "John" ]
!=non equal text strings[ $name1 != $name2 ]
-eqequal integers[ $age -eq $adult ]
-gtinteger1 is greater than integer2[ $age -gt 20 ]
-geinteger1 is greater than or equal to integer2[ $age -ge 21 ]
-ltinteger1 is less than ...

Get Spring Into Linux® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.