test — Performs test operations
test
[--help] [--version] expression
--help
Display this help and exit
--version
Output version information and exit
expression
Expression to evaluate.
Exit with the status determined by the expression (shown in Table 8, “Possible test expressions”).
An omitted expression defaults to false. Otherwise, the expression is evaluated to a true or false result and sets exit status accordingly.
An alias is provided so this command can be called as [ this gives a more familiar way to use this command.
For the tests which check for a file having UNIX® read or write permissions ABLE assumes the root (UID 0 and GID 0) superuser is being used.
Table 8. Possible test expressions
Test | Result is true if |
---|---|
( expression ) | expression is
true |
! expression | expression is
false |
expression1 -a
expression2 | both expression1 and
expression2 are
true |
expression1 -o
expression2 | either expression1 or
expression2 is true |
-n string | the length of string
is nonzero |
string | equivalent to -n
string |
-z string | the length of string
is zero |
string1 =
string2 | the strings are equal |
string1 !=
string2 | the strings are not equal |
integer1 -eq
integer2 | integer1 is equal to
integer2 |
integer1 -ge
integer2 | integer1 is greater
than or equal to
integer2 |
integer1 -gt
integer2 | integer1 is greater
than integer2 |
integer1 -le
integer2 | integer1 is less than
or equal to
integer2 |
integer1 -lt
integer2 | integer1 is less than
integer2 |
integer1 -ne
integer2 | integer1 is not equal
to integer2 |
file1 -ef
file2 | file1 and
file2 have the same device
and inode numbers |
file1 -nt
file2 | file1 is newer
(modification date) than
file2 |
file1 -ot
file2 | file1 is older than
file2 |
-b file | file exists and is
block special |
-c file | file exists and is
character special |
-d file | file exists and is a
directory |
-e file | file exists |
-f file | file exists and is a
regular file |
-g file | file exists and is
set-group-ID |
-G file | file exists and is
owned by the effective group ID |
-h file | file exists and is a
symbolic link (same as -L) |
-k file | file exists and has
its sticky bit set |
-L file | file exists and is a
symbolic link (same as -h) |
-O file | file exists and is
owned by the effective user ID |
-p file | file exists and is a
named pipe |
-r file | file exists and read
permission is granted |
-s file | file exists and has a
size greater than zero |
-S file | file exists and is a
socket |
-t fd | file descriptor fd is
opened on a terminal |
-u file | file exists and its
set-user-ID bit is set |
-w file | file exists and write
permission is granted |
-x file | file exists and
execute (or search) permission is granted |
Except for -h
and -L
,
all file-related tests dereference symbolic links. Beware
that parentheses need to be escaped (e.g., by back-slashes)
for shells.
Example 52. Performing assorted tests
This example shows the use of the test command with various expressions. Each test uses “&& echo true” to make it clear when the expression returns a true (zero) exit status.
>test ! ( 1 -ge 0 ) && echo true >echo $? 1 >test ( 1 -ge 0 ) && echo true true >echo $? 0 >test ! ( 1 -ge 0 ) && echo true >test ! ( 0 -ge 1 ) && echo true true >test -f (hd0)/etc/services && echo true true >test -f (hd0)/etc/services -a ( 1 -ge 0 ) && echo true true >test -f (hd0)/etc/services -a ( 1 -ge 2 ) && echo true >test -f (hd0)/etc/services -o ( 1 -ge 2 ) && echo true true >test -n "foo" && echo true true >test -n "" && echo true >test -z "foo" && echo true >test "foo" = "foo" && echo true true >test "foo" = "bar" && echo true >test "foo" != "bar" && echo true true >test 1 -eq 2 && echo true >test 1 -eq 1 && echo true true >test 1 -ge 0 && echo true true >test 1 -ge 1 && echo true true >test 1 -ge 2 && echo true >test 1 -gt 1 && echo true >test 1 -le 2 && echo true true >test 1 -le 1 && echo true true >test 1 -le 0 && echo true >test 1 -lt 1 && echo true >test 1 -ne 1 && echo true >test 1 -ne 0 && echo true true >test -f (hd0)/etc/services >test -f (hd0)/etc/services && echo true true >test -d (hd0)/etc/services && echo true >test -d (hd0)/etc && echo true true >test -b (hd0)/etc/services && echo true >test -b (hd0)/dev/hda && echo true true >test -c (hd0)/dev/hda && echo true test -c (hd0)/dev/tty && echo true true >