Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

[ ] - evaluate expression

&pagelevel(4)&pagelevel


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:

$ cat dr

for name in *

do if [ -d "$name" ]

   then echo "(dir) $name"

   else echo " `ls -s $name`"
   fi

done

$ dr
    2 order

(dir) letters

    2 dr
    2 notes

(dir) cards

(dir) docoments

For further information and examples refer to the test command.

See also

test