test, [, [[test for condition |
Command |
test
expression
[
expression ]
[[
expression ]]
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
is identical to the[ expression ]
test
command
The [[
command
features some different operators than the[[ expression ]]
test
and
[
commands.
test
, [
, and [[
accept the following operators:
-b
filetrue if the file is a block special file
-c
filetrue if the file is a character special file
-d
filetrue if the file is a directory
-e
filetrue if the file exists
-f
filetrue if the file is an ordinary file
-g
filetrue if the setgid attribute of the file is on under UNIX
-h
filetrue if file is a symbolic link
-k
filetrue if the save text attribute of the file is on under UNIX
-L
filetrue if file is a symbolic link
-n
stringtrue if the length of string is greater than zero
-p
filetrue if the file is a FIFO (named pipe)
-r
filetrue if the file is readable
-s
filetrue if the size of the file is non-zero
-t
fdtrue if the numeric file descriptor fd is open and associated with a terminal
-u
filetrue if the setuid attribute of the file is on under UNIX
-w
filetrue if the file is writable
-x
filetrue if the file is executable
-z
stringtrue if the length of the string is zero
-eq
number2true if number1 and number2 are equal
Both number1 and number2 must be integers-ge
number2true if number1 is greater than or equal to number2
-gt
numbertrue if number1 is greater than number2
-le
number2true if the first number1 is less than or equal to number2
-lt
number2true if number1 is less than number2
-ne
number2true if number1 is not equal to number2
-nt
file2true if file1 is newer than file2
-ot
file2true if file1 is older than file2
-ef
file2true if file1 has the same device and i-node number as file2
test
and [
commands:
true if string is not a null string
=
string2true if string1 and string2 are identical
!=
string2true if string1 and string2 are not identical
[[
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.
==
pattern =
patterntrue if string matches pattern.
!=
patterntrue if string does not match pattern.
<
string2true if string1 comes before string2 alphabetically.
>
string2true if string1 comes after string2 alphabetically.
-a
expr2 (test
and [
commands)&&
expr2
([[
command)logical AND; true if both expr1 and expr2 are true
-o
expr2 (test
and [
commands)||
expr2 ([[
command)logical OR; true if either expr1 or expr2 is true
!
exprlogical negation; true if expr is false
(
expr )
binding; true if expr is true
This example illustrates the use ofif [ -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
test
and is not
intended to be an efficient method.
0
The expression was true.
1
The expression was false or missing.
2
The expression was badly formed.
-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.
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,
Iftest $NULL != string
NULL
is undefined or empty, this results in
which is not a valid test expression. This problem can be fixed by enclosingtest != string
$NULL
in quotes.
stat