Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was looking at coreutils and found this as one of the files included as part of coreutils: /usr/bin/[. What is [ and what does it do?

It is an executable. I just don't know what it does or how to use it.

$ file /usr/bin/[
/usr/bin/[: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

When I try to run it, I think it is defaulting to the bash built in line expansion. Instead of actually running the file.

$ "/usr/bin/["
/usr/bin/[: missing ]' $ /usr/bin/\[
/usr/bin/[: missing

share|improve this question
man [ works on OS X. – Daniel Beck Sep 12 '11 at 17:31
up vote 30 down vote accepted

It's an equivalent of the command test. (See info test.) Generally you use it in scripts in conditional expressions like:

if [ -n "$1" ]; then
    echo $1

The closing bracket is required to enclose the conditional. (Well, it looks like its required just to look nicer in the code. Does anybody know any other practical reason for it?)

share|improve this answer
Note that [ is both a shell built-in and a external program with the same (or similar) usage. In bash, when you run [ or test you are invoking the built-in. – grawity Sep 12 '11 at 7:29
It's required because if it were optional the syntax would be ambiguous in certain situations. – Random832 Sep 12 '11 at 11:57

It is equivalent to the test command.

Instead of

if /usr/bin/test -z "$VAR"
    echo VAR not set

You can use:

if /usr/bin/[ -z "$VAR" ]
    echo VAR not set

It can be used in loops too:

while [ $i -lt 10 ]
   echo $i

You can also use them in one-liners like this:

[ -z "$VAR" ] && echo VAR not set && exit

[ -f foo.txt ] && cat foo.txt
share|improve this answer
nice examples of how to use it. "if /usr/bin/[" – nelaar Sep 12 '11 at 11:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.