UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: suspendChapter 4
The Bourne Shell and Korn Shell
Next: Reference: time
 

test

test condition
     or
[ 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).

File Conditions

-a file

file exists. (K)

-b file

file exists and is a block special file.

-c file

file exists and is a character special file.

-d file

file exists and is a directory.

-f file

file exists and is a regular file.

-g file

file exists, and its set-group-id bit is set.

-G file

file exists, and its group is the effective group ID. (K)

-k file

file exists, and its sticky bit is set.

-L file

file exists and is a symbolic link. (K)

-o c

Option c is on. (K)

-O file

file exists, and its owner is the effective user ID. (K)

-p file

file exists and is a named pipe (fifo).

-r file

file exists and is readable.

-s file

file exists and has a size greater than zero.

-S file

file exists and is a socket. (K)

-t [n]

The open file descriptor n is associated with a terminal device; default n is 1.

-u file

file exists, and its set-user-id bit is set.

-w file

file exists and is writable.

-x file

file exists and is executable.

f1 -ef f2

Files f1 and f2 are linked (refer to same file). (K)

f1 -nt f2

File f1 is newer than f2. (K)

f1 -ot f2

File f1 is older than f2. (K)

String Conditions

string

string is not null.

-n s1

String s1 has nonzero length.

-z s1

String s1 has zero length.

s1 = s2

Strings s1 and s2 are identical. In the Korn shell, s2 can be a wildcard pattern. (See the section "Filename Metacharacters," earlier in this chapter.)

s1 == s2

Strings s1 and s2 are identical. s2 can be a wildcard pattern. Preferred over =. (K93)

s1 != s2

Strings s1 and s2 are not identical. In the Korn shell, s2 can be a wildcard pattern.

s1 < s2

ASCII value of s1 precedes that of s2. (Valid only within [[]] construct). (K)

s1 > s2

ASCII value of s1 follows that of s2. (Valid only within [[]] construct). (K)

Integer Comparisons

n1 -eq n2

n1 equals n2.

n1 -ge n2

n1 is greater than or equal to n2.

n1 -gt n2

n1 is greater than n2.

n1 -le n2

n1 is less than or equal to n2.

n1 -lt n2

n1 is less than n2.

n1 -ne n2

n1 does not equal n2.

Combined Forms

(condition)

True if condition is true (used for grouping). The ()s should be quoted by a \.

! condition

True if condition is false.

condition1 -a condition2

True if both conditions are true.

condition1 && condition2

True if both conditions are true. (Valid only within [[]] construct.) (K)

condition1 -o condition2

True if either condition is true.

condition1 || condition2

True if either condition is true. (Valid only within [[]] construct.) (K)

Examples

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...


Previous: Reference: suspendUNIX in a Nutshell: System V EditionNext: Reference: time
Reference: suspendBook IndexReference: time

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System