The POSIX shell built-in [
] is used to check whether specific conditions are satisfied.
Such conditions may be:
file attributes,
characteristics and comparisons of strings, and
algebraic comparisons of integers.
Conditions can be both combined and negated.
[
] returns the following results::
Exit status 0 (true) if the condition is satisfied.
Exit status 1 (false), if the condition is not satisfied or was not fully defined. A false exit status is also returned if you do not specify any condition.
Depending on the exit status, you can execute various commands, terminate loops, etc.
The shell built-in [
] has two forms (see syntax ). The effect is the same in both cases.
Syntax
test expression |
[ expression ]
|
See the shell built-in test for the syntax description. |
Example
The shell script dr produces a customized listing of the contents of the current directory. It tests each entry in the directory with the command [ -d "$name" ] to find out whether it is a subdirectory. Depending on the result of this test, it lists the name preceded either by the label "(dir)" or by the size of the file in blocks:
For further information and examples refer to the test command. |
See also
test |