25

I was just poking around in /usr/bin and I found an ELF binary file called [. /usr/bin/[. I have never heard of this file and my first thought was that it was a clever way of hiding a program, possibly a trojan. However it's present on all my CentOS servers and seems to have no manual entry. I can hazard a guess as to what it is but I was looking for a more authoritative answer...

Clint Miller
  • 1,141
  • 1
  • 11
  • 19
Josh
  • 9,001
  • 27
  • 78
  • 124

3 Answers3

28

It's an alternative form of the 'test' command. Mostly used in scripts.

i.e.

if [ $VAR ]
then
    echo $VAR exists!
fi
Zypher
  • 36,995
  • 5
  • 52
  • 95
  • 3
    But unlike test it requires the last arg to be a ] – Florian Diesch May 05 '10 at 19:09
  • 1
    Thanks. That's what I figured but I was surprised because I thought [ was a bash builtin – Josh May 05 '10 at 19:12
  • 4
    `[` *is* a bash builtin, but so is `test`. not all shells are created equal — in plenty of them, `test` (and `[`) aren’t builtins. – Mo. May 05 '10 at 19:27
  • 3
    There is a bash built-in, or at least, my system is acting as though there is. [ --help gives different input than /usr/bin/[ --help – Matt Simmons May 05 '10 at 19:27
  • 3
    test and [ are builtin to bash, but not necessarily all other shells. You could temporarily move out of the path and run a bash script that uses either and you will see that the script still works. – Zoredache May 05 '10 at 19:28
  • 1
    Or try `type -a [` which will show that `[` is a Bash builtin and `/usr/bin/[`. The order shown is the order of preference. – Dennis Williamson May 05 '10 at 19:43
  • 1
    This must be the most upvoted string of comments ever, lol. – Josh May 05 '10 at 21:19
  • 1
    `[ $VAR ]` fails when `$VAR` is empty or not set. Use `[ "$VAR" ]` instead. – user1686 May 06 '10 at 11:59
  • 3
    @Josh: once upon another millennium, the Bourne shell did not have the test operator built in. It was a regular command like any other; and /bin/test was linked to /bin/[ to give notational convenience. – Jonathan Leffler May 06 '10 at 15:05
  • This encapsulates everything I despise about software. – iono Aug 26 '22 at 20:23
10

It's what you call when you are using something like

if [ -e foo ]; then ...

in a shell script (but most shells have it as a buildin this days). man test should give you the docs.

Florian Diesch
  • 1,794
  • 1
  • 11
  • 5
4

As others pointed out, [ is the shell's condition evaluation utility - test.

In fact, there is a manual page for that!

$ man [

should give you more details about the opening square bracket.

Btw, in OS X, [ is located in /bin/[ :)

Devy
  • 169
  • 3