[ expression ]
[[ expression ]]
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.
When the set
-o kornoption 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 kornis not set, such extra arguments result in an error.
test, [, and [[ accept the following operators:
true if the file is a block special file
true if the file is a character special file
true if the file is a directory
true if the file exists
true if the file is an ordinary file
true if the setgid attribute of the file is on under UNIX (or the system attribute on Windows systems)
true if file is a symbolic link
true if the save text attribute of the file is on under UNIX (or the archive attribute on Windows systems)
true if file is a symbolic link
true if the length of string is greater than zero
true if the file is a FIFO (named pipe)
true if the file is readable
true if the size of the file is non-zero
true if the numeric file descriptor fd is open and associated with a terminal
true if the setuid attribute of the file is on under UNIX (or the hidden attribute on Windows systems)
true if the file is writable
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,
-xreturns true. -zstring
true if the length of the string is zero
true if number1 and number2 are equal
Both number1 and number2 must be integers
true if number1 is greater than or equal to number2
true if number1 is greater than number2
true if the first number1 is less than or equal to number2
true if number1 is less than number2
true if number1 is not equal to number2
true if file1 is newer than file2
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.
true if file1 is older than file2
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.
true if file1 has the same device and i-node number as file2
The following operators handle string comparisons for the test and [ commands:
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:
-aexpr2 (test and [ commands)
- expr1 && expr2 ([[ command)
logical AND; true if both expr1 and expr2 are true
-oexpr2 (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:
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.
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).
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.
Possible exit status values are:
The expression was true.
The expression was false or missing.
The expression was badly formed.
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.
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.
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
Using the MKS KornShell
PTC MKS Toolkit 9.6 patch 1 Documentation Build 5.