[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 5

test Command

Purpose

Evaluates conditional expressions.

Syntax

test Expression

OR

[ Expression ]

Description

The test command evaluates the Expression parameter, and if the expression value is True, returns a zero (True) exit value. Otherwise, the test command returns a nonzero (False) exit value. The test command also returns a nonzero exit value if there are no parameters.

Notes:
  1. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces.
  2. You must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit.

Functions and operators are treated as separate parameters by the test command. The Expression parameter refers to a statement that is checked for a true or false condition. The following functions are used to construct this parameter:

-b FileName Returns a True exit value if the specified FileName exists and is a block special file.
-c FileName Returns a True exit value if the specified FileName exists and is a character special file.
-d FileName Returns a True exit value if the specified FileName exists and is a directory.
-e FileName Returns a True exit value if the specified FileName exists.
-f FileName Returns a True exit value if the specified FileName exists and is a regular file.
-g FileName Returns a True exit value if the specified FileName exists and its Set Group ID bit is set.
-h FileName Returns a True exit value if the specified FileName exists and is a symbolic link.
-k FileName Returns a True exit value if the specified FileName exists and its sticky bit is set.
-L FileName Returns a True exit value if the specified FileName exists and is a symbolic link.
-n String1 Returns a True exit value if the length of the String1 variable is nonzero.
-p FileName Returns a True exit value if the specified FileName exists and is a named pipe (FIFO).
-r FileName Returns a True exit value if the specified FileName exists and is readable by the current process.
-s FileName Returns a True exit value if the specified FileName exists and has a size greater than 0.
-t FileDescriptor Returns a True exit value if the file with a file descriptor number of FileDescriptor is open and associated with a terminal.
-u FileName Returns a True exit value if the specified FileName exists and its Set User ID bit is set.
-w FileName Returns a True exit value if the specified FileName exists and the write flag is on. However, the FileNamewill not be writable on a read-only file system even if test indicates true.
-x FileName Returns a True exit value if the specified FileName exists and the execute flag is on. If the specified file exists and is a directory, the True exit value indicates that the current process has permission to search in the directory.
-z String1 Returns a True exit value if the length of the String1 variable is 0 (zero).
String1= String2 Returns a True exit value if the String1 and String2 variables are identical.
String1!=String2 Returns a True exit value if the String1 and String2 variables are not identical.
String1 Returns a True exit value if the String1 variable is not a null string.
Integer1 -eq Integer2 Returns a True exit value if the Integer1 and Integer2 variables are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq.
file1 -nt file2 True if file1 is newer than file2.
file1 -ot file2 True if file1 is older than file2.
file1 -ef file2 True if file1 is another name for file2.

These functions can be combined with the following operators:

! Unary negation operator
-a Binary AND operator
-o Binary OR operator ( that is, the -a operator has higher precedence than the -o operator)
\(Expression\) Parentheses for grouping

Exit Status

This command returns the following exit values:

0 The Expression parameter is true.
1 The Expression parameter is false or missing.
>1 An error occurred.

Examples

  1. To test whether a file exists and is not empty, type:

    if test ! -s "$1"
    then
       echo $1 does not exist or is empty.
    fi

    If the file specified by the first positional parameter to the shell procedure, $1, does not exist, the test command displays an error message. If $1 exists and has a size greater than 0, the test command displays nothing.

    Note: There must be a space between the -s function and the file name.

    The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message test: argument expected.

  2. To do a complex comparison, type:

    if [ $# -lt 2  -o  ! -e "$1" ]
    then
       exit
    fi

    If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure.

The Shells Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices describes shells in general, defines terms that are helpful in understanding shells, and describes the more useful shell functions.

File

/usr/bin/test Contains the test command.

Related Information

The bsh command, csh command, find command, ksh command, sh command.

Shells Overview in AIX 5L Version 5.2 System User's Guide: Operating System and Devices.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]