Index Click this button to go to the index for this section.


test(1)

NAME

test, [ - Evaluates conditional expressions

SYNOPSIS

test [expression] [[expression]]

STANDARDS

Interfaces documented on this reference page conform to industry standards as follows: test: XPG4, XPG4-UNIX [: XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags.

OPTIONS

None

OPERANDS

expression The expression to be evaluated as true or false. This expression is constructed from the operators and elements explained in the DESCRIPTION section.

DESCRIPTION

The test command evaluates an expression constructed of functions and operators. If the value of expression is true, test returns an exit value of zero (0); otherwise, it returns FALSE, a nonzero exit value. The test command also returns a nonzero exit value if there are no arguments. The alternate form of the command surrounds expression with brackets ([ ]). When you use this form, you must surround the brackets with spaces. The test Expressions All of the listed functions and operators are separate arguments to test. The following functions are used to construct expression: -r file TRUE if file exists and has read permission. -w file TRUE if file exists and has write permission. -x file TRUE if file exists and has execute permission. -f file TRUE if file exists and is a regular file. -d file TRUE if file exists and is a directory. -e file TRUE if file exists. -c file TRUE if file exists and is a character-special file. -b file TRUE if file exists and is a block-special file. -p file TRUE if file exists and is a named pipe (FIFO). -h file [DIGITAL] TRUE if file exists and is a soft link. Synonym for -L expression. -L file TRUE if file exists and is a soft link. Synonym for -h expression. -u file TRUE if file exists and its set-user ID bit is set. -g file TRUE if file exists and its set-group ID bit is set. -k file [DIGITAL] TRUE if file exists and its sticky bit is set. -s file TRUE if file exists and has a size greater than zero (0). -t [file_descriptor] TRUE if the open file with file descriptor number file_descriptor (1 by default) is associated with a terminal device. -z string1 TRUE if the length of string1 is zero (0). -n string1 TRUE if the length of string1 is nonzero. string1 = string2 TRUE if string1 and string2 are identical. string1 != string2 TRUE if string1 and string2 are not identical. string1 TRUE if string1 is not the null string. number1 -eq number2 TRUE if the integers number1 and number2 are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, and -le can be used in place of -eq. The listed functions can be combined with the following operators: ! Unary negation operator. -a Binary AND operator. -o Binary OR operator (the -a operator has higher precedence than the -o operator). \( expression \) [DIGITAL] Parentheses for grouping.

EXIT STATUS

The following exit values are returned: 0 The test command evaluated expression and its value is TRUE. 1 The test command evaluated expression and is value is FALSE, or there are no arguments. >1 An error occurred.

EXAMPLES

1. To test whether a file exists and is not empty, enter: 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 does not exist, this displays an error message. If $1 exists, it displays nothing. There must be a space between -s expression and the file name. The double quotes around $1 ensure that the test will work properly even if the value of $1 is the empty string. If the double quotes are omitted and $1 is the empty string, test displays the error message test: parameter expected. 2. To do a complex comparison, enter: if [ $# -lt 2 -o ! -s "$1" ] then exit fi If the shell procedure was given fewer than two positional parameters or the file specified by $1 does not exist, then this exits the shell procedure. The special shell variable $# represents the number of positional parameters entered on the command line that started this shell procedure.

ENVIRONMENT VARIABLES

The following environment variables affect the execution of test: LANG Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization variables contain an invalid setting, the utility behaves as if none of the variables had been defined. LC_ALL If set to a non-empty string value, overrides the values of all the other internationalization variables. LC_CTYPE Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments). LC_MESSAGES Determines the locale for the format and contents of diagnostic messages written to standard error. NLSPATH Determines the location of message catalogues for the processing of LC_MESSAGES.

SEE ALSO

Commands: csh(1), find(1), ksh(1), Bourne shell sh(1b), POSIX shell sh(1p) Standards: standards(5)