Tests
test
test expression
or
[ expression ]
Test returns 0 (zero!) if expression is true and >0 if it is false.
Examples
Testing for empty string:
test -z "$variable"
[ -z "$variable" ]
[ x$variable != x ]
- "[" is a command like any other --> "]" is a argument to "[" and must be separated by a whitespace from the penultimate argument.
- what would happen if the quotes in the first two example were ommitted?
- the third example is actually an example to write the test without quotes
-
test if two strings are equal
test "$variable" = "hello world"
test if two numbers are equal
test $number -eq 3
test if $filename exists and is a regular file
test -f $filename
test if $filename exists and is a regular file AND is not executable
test -f $filename -a ! -x $filename
test if $number is greater than 7
test $number -gt 7