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 non-zero 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 expressions below. Conditions are true if the description holds true. Features that are Korn-shell-specific are marked with a (K).

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 group is the effective group ID. (K)

-g file

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

-k file

file exists and its sticky bit is set.

-L file

file exists and is a symbolic link. (K)

-O file

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

-o c

Option c is on. (K)

-p file

file exists and is a named pipe (fifo).

-r file

file exists and is readable.

-S file

file exists and is a socket. (K)

-s file

file exists and has a size greater than zero.

-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

-n s1

String s1 has non-zero length.

-z s1

String s1 has zero length.

s1 = s2

Strings s1 and s2 are identical. In the Korn shell, s2 can be a regular expression.

s1 != s2

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

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)

string

string is not null.

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 preceded by a \.

"!

True if condition is false.

"condition1

True if both conditions are true.

condition1

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

condition1

True if either condition is true.

condition1

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

Examples

Each example below shows 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