The Bourne shell has a number of control constructs to permit branching and looping. Several of them involve logical expressions so we will deal with them first. Logical expressions involve unary (single operand) and binary (two operand) operators. The table below lists some of the most useful together with their C shell equivalents.
Op | Op | Description | sh Example |
(sh) | (csh) | ||
-eq | == | numeric equality | $var -eq 3 |
-ne | != | numeric inequality | $var -ne 3 |
-lt | numeric less than | $var -lt 3 | |
-le | numeric less than or equal | $var -le 3 | |
-gt | numeric greater than | $var -gt 3 | |
-ge | numeric greater than or equal | $var -ge 3 | |
= | == | string equality | $var = "3" |
!= | != | string inequality | $var != "3" |
-a | && | logical and | $var -eq 3 -a $var -eq 4 |
-o | || | logical or | $var -eq 3 -o $var -eq 4 |
! | ! | logical not | ! ($var -eq 3 -o $var -eq 4) |
-r | -r | file test: is readable | -r myfile |
-w | -w | file test: is writable | -w myfile |
-s | -s | file test: is not empty | -s myfile |
-d | -d | file test: is a directory | -d myfile |
-f | -f | file test: is a file (not dir) | -f myfile |
Use !, -a, -o (in csh !, && ||) and parentheses to construct more complex expressions from simpler ones.