Name

test — Performs test operations

Synopsis

test [--help] [--version] expression

Options

--help

Display this help and exit

--version

Output version information and exit

expression

Expression to evaluate.

Description

Exit with the status determined by the expression (shown in Table 10.2, “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 10.2. Possible test expressions

TestResult is true if
( expression ) expression is true
! expressionexpression is false
expression1 -a expression2both expression1 and expression2 are true
expression1 -o expression2either expression1 or expression2 is true
-n stringthe length of string is nonzero
string equivalent to -n string
-z stringthe length of string is zero
string1 = string2the strings are equal
string1 != string2the strings are not equal
integer1 -eq integer2integer1 is equal to integer2
integer1 -ge integer2integer1 is greater than or equal to integer2
integer1 -gt integer2integer1 is greater than integer2
integer1 -le integer2integer1 is less than or equal to integer2
integer1 -lt integer2integer1 is less than integer2
integer1 -ne integer2integer1 is not equal to integer2
file1 -ef file2file1 and file2 have the same device and inode numbers
file1 -nt file2file1 is newer (modification date) than file2
file1 -ot file2file1 is older than file2
-b filefile exists and is block special
-c filefile exists and is character special
-d filefile exists and is a directory
-e filefile exists
-f filefile exists and is a regular file
-g filefile exists and is set-group-ID
-G filefile exists and is owned by the effective group ID
-h filefile exists and is a symbolic link (same as -L)
-k filefile exists and has its sticky bit set
-L filefile exists and is a symbolic link (same as -h)
-O filefile exists and is owned by the effective user ID
-p filefile exists and is a named pipe
-r filefile exists and read permission is granted
-s filefile exists and has a size greater than zero
-S filefile exists and is a socket
-t fd file descriptor fd is opened on a terminal
-u filefile exists and its set-user-ID bit is set
-w filefile exists and write permission is granted
-x filefile 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 10.5. 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
>