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.
Option | Tests for | Example |
---|---|---|
= | equal text strings | [ $name = "John" ] |
!= | non equal text strings | [ $name1 != $name2 ] |
-eq | equal integers | [ $age -eq $adult ] |
-gt | integer1 is greater than integer2 | [ $age -gt 20 ] |
-ge | integer1 is greater than or equal to integer2 | [ $age -ge 21 ] |
-lt | integer1 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.