test, [, [[

test for condition

Command


SYNOPSIS

test expression

[ expression ]

[[ expression ]]


DESCRIPTION

The test command checks for various properties of files, strings and integers. It produces no output (except error messages) but returns the result of the test as the exit status; see DIAGNOSTICS for more information.

The command line includes a Boolean expression. The simplest expression is a string which is true if the string is non-empty (that is, has non-zero length). More complex expressions are composed of operators and operands, each of which is a separate argument (that is, surrounded by white space). The operators imply the number and type of their operands. The operators taking a file operand evaluate as false (without error) if the file does not exist.

The [ command
[ expression ]
is identical to the test command

The [[ command
[[ expression ]]
features some different operators than the test and [ commands.

Operators

test, [, and [[ accept the following operators:
-b file

true if the file is a block special file

-c file

true if the file is a character special file

-d file

true if the file is a directory

-e file

true if the file exists

-f file

true if the file is an ordinary file

-g file

true if the setgid attribute of the file is on under UNIX

-h file

true if file is a symbolic link

-k file

true if the save text attribute of the file is on under UNIX

-L file

true if file is a symbolic link

-n string

true if the length of string is greater than zero

-p file

true if the file is a FIFO (named pipe)

-r file

true if the file is readable

-s file

true if the size of the file is non-zero

-t fd

true if the numeric file descriptor fd is open and associated with a terminal

-u file

true if the setuid attribute of the file is on under UNIX

-w file

true if the file is writable

-x file

true if the file is executable

-z string

true if the length of the string is zero

number1 -eq number2

true if number1 and number2 are equal

Both number1 and number2 must be integers

number1 -ge number2

true if number1 is greater than or equal to number2

number -gt number

true if number1 is greater than number2

number1 -le number2

true if the first number1 is less than or equal to number2

number1 -lt number2

true if number1 is less than number2

number1 -ne number2

true if number1 is not equal to number2

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 has the same device and i-node number as file2

The following operators handle string comparisons for the test and [ commands:
string

true if string is not a null string

string1 = string2

true if string1 and string2 are identical

string1 != string2

true if string1 and string2 are not identical

The [[ command has several operators which handle string comparisons and pattern-matching. In the following descriptions, pattern is a glob pattern as described in the File Name Generation section of sh. To treat pattern as a string, quote it.
string == pattern
string = pattern

true if string matches pattern.

string != pattern

true if string does not match pattern.

string1 < string2

true if string1 comes before string2 alphabetically.

string1 > string2

true if string1 comes after string2 alphabetically.

The following operators allow you to combine other operators:
expr1 -a expr2 (test and [ commands)
expr1 && expr2 ([[ command)

logical AND; true if both expr1 and expr2 are true

expr1 -o expr2 (test and [ commands)
expr1 || expr2 ([[ command)

logical OR; true if either expr1 or expr2 is true

! expr

logical negation; true if expr is false

( expr )

binding; true if expr is true

The precedence of the operators, in descending order, is:

EXAMPLES

The following command reports on whether the first positional parameter contains a directory or a file:
if [ -f $1 ]
then
  echo $1 is a file
elif [ -d $1 ]
then
  echo $1 is a directory
else
  echo $1 neither file nor directory
fi
This example illustrates the use of test and is not intended to be an efficient method.


DIAGNOSTICS

Possible exit status values are:
0

The expression was true.

1

The expression was false or missing.

2

The expression was badly formed.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0.

The -k, -L, -nt, -ot, -ef, -a and -o operators, plus the use of parentheses to group operators together, are all extensions to the POSIX standard.

The -k, -L, -nt, -ot, and -ef operators are extensions to the x/OPEN standard.


NOTE

test, [, and [[ are built into the MKS KornShell. test is also implemented as a separate utility.

Failure to quote variable expansions is a common mistake. For example,
test $NULL != string
If NULL is undefined or empty, this results in
test != string
which is not a valid test expression. This problem can be fixed by enclosing $NULL in quotes.


SEE ALSO

Commands:
expr, find, let, ls, sh

Miscellaneous:
stat


Updated MKS Toolkit [3khat16.ico]HP3000 [3khat16.ico]3kMail [archive16.gif]