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.

Note:

When the set -o korn option is set and the TK_TEST_CHECK_FOR_EXTRA_ARGS environment variable is not set, test, [, and [[ behave as in the standard Korn Shell and quietly ignore any extra specified arguments beyond those required for the specified operator. When set -o korn is not set, such extra arguments result in an error.

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 (or the system attribute on Windows systems)

-h file 

true if file is a symbolic link

-k file 

true if the save text attribute of the file is on under UNIX (or the archive attribute on Windows systems)

-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 (or the hidden attribute on Windows systems)

-w file 

true if the file is writable

-x file 

true if the file is executable. If the specified file does not exist, test checks each extension in the PATHEXT environment variable (in the order listed) to see if a file exists with the specified name plus that extension. If such a file does exist and it is executable, -x returns true.

-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

Note:

All existing files are considered to be newer than a nonexistent file. In turn, no nonexistent file is newer than any existing file. Two nonexistent files are considered the same age.

file1 -ot file2 

true if file1 is older than file2

Note:

A nonexistent file is considered to be older than any existing file. In turn, no existing file is older than a nonexistent file. Two nonexistent files are considered the same age.

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.


ENVIRONMENT VARIABLES

PATHEXT 

contains a list of file extensions (separated by semicolons) for executable commands. Matching files with these extensions are searched for when an exact file name is not found with -x. As the shell searches each directory in the search path, it appends each of the extensions in the list, in turn, to the specified file name and if it matches a file name in that directory, that file is checked to see if it is executable.

By default, PATHEXT, has the value of XP/2003/Vista/7/2008/8/2012's PATHEXT variable with .com;.exe;.bat;.sh;.ksh;.csh;.sed;.awk;.pl appended (omitting any extensions already represented in PATHEXT).

TK_TEST_CHECK_FOR_EXTRA_ARGS 

When set, test always checks to see if more arguments are present than are needed for the specified operator. If there are too many arguments, an error results.


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. All UNIX systems. Windows XP. Windows Server 2003. Windows Vista. Windows 7. Windows Server 2008. Windows 8. Windows Server 2012.

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.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
expr, find, let, ls, sh

Miscellaneous:
stat

Using the MKS KornShell


PTC MKS Toolkit 9.6 Documentation Build 9.