test
condition[
condition ]
Evaluate a condition and, if its value is true, return a zero exit status; otherwise, return a nonzero exit status. An alternate form of the command uses [ ]
rather than the word test
. The Korn shell allows an additional form, [[ ]]
. condition is constructed using the following expressions. Conditions are true if the description holds true. Features that are specific to the Korn shell are marked with a (K). Features that are specific to ksh93 are marked with a (K93).
-a
filefile exists. (K)
-b
filefile exists and is a block special file.
-c
filefile exists and is a character special file.
-d
filefile exists and is a directory.
-f
filefile exists and is a regular file.
-g
filefile exists, and its set-group-id bit is set.
-G
filefile exists, and its group is the effective group ID. (K)
-k
filefile exists, and its sticky bit is set.
-L
filefile exists and is a symbolic link. (K)
-o
cOption c is on. (K)
-O
filefile exists, and its owner is the effective user ID. (K)
-p
filefile exists and is a named pipe (fifo).
-r
filefile exists and is readable.
-s
filefile exists and has a size greater than zero.
-S
filefile exists and is a socket. (K)
-t
[n]The open file descriptor n is associated with a terminal device; default n is 1.
-u
filefile exists, and its set-user-id bit is set.
-w
filefile exists and is writable.
-x
filefile exists and is executable.
-ef
f2Files f1 and f2 are linked (refer to same file). (K)
-nt
f2File f1 is newer than f2. (K)
-ot
f2File f1 is older than f2. (K)
string is not null.
-n
s1String s1 has nonzero length.
-z
s1String s1 has zero length.
=
s2Strings s1 and s2 are identical. In the Korn shell, s2 can be a wildcard pattern. (See the section "Filename Metacharacters," earlier in this chapter.)
==
s2Strings s1 and s2 are identical. s2 can be a wildcard pattern. Preferred over =
. (K93)
!=
s2Strings s1 and s2 are not identical. In the Korn shell, s2 can be a wildcard pattern.
<
s2ASCII value of s1 precedes that of s2. (Valid only within [[]]
construct). (K)
>
s2ASCII value of s1 follows that of s2. (Valid only within [[]]
construct). (K)
-eq
n2n1 equals n2.
-ge
n2n1 is greater than or equal to n2.
-gt
n2n1 is greater than n2.
-le
n2n1 is less than or equal to n2.
-lt
n2n1 is less than n2.
-ne
n2n1 does not equal n2.
(
condition)
True if condition is true (used for grouping). The ()
s should be quoted by a \
.
!
conditionTrue if condition is false.
-a
condition2True if both conditions are true.
&&
condition2True if both conditions are true. (Valid only within [[]]
construct.) (K)
-o
condition2True if either condition is true.
||
condition2True if either condition is true. (Valid only within [[]]
construct.) (K)
The following examples show the first line of various statements that might use a test condition:
while test $# -gt 0 While there are arguments... while [ -n "$1" ] While there are nonempty arguments... if [ $count -lt 10 ] If $count is less than 10... if [ -d RCS ] If the RCS directory exists... if [ "$answer" != "y" ] If the answer is not y... if [ ! -r "$1" -o ! -f "$1" ] If the first argument is not a readable file or a regular file...