Linux Command Line – Differences Between `test` and `[` Binaries

command linecoreutilslinuxtestUtilities

I noticed while answering another question that test and [ are different binaries, but the [ manpage pulls up test's. Besides the requirement for an ending ], is there any difference? If not, why are they separate binaries instead of being symlinked? (They are also bash builtins, and bash doesn't show a difference either.)

Best Answer

The source code explains the difference as being how it handles the --help option.

  /* Recognize --help or --version, but only when invoked in the
     "[" form, when the last argument is not "]".  Use direct
     parsing, rather than parse_long_options, to avoid accepting
     abbreviations.  POSIX allows "[ --help" and "[ --version" to
     have the usual GNU behavior, but it requires "test --help"
     and "test --version" to exit silently with status 0.  */

Demonstrating

$ /usr/bin/test --help
$
$ /usr/bin/[ --help
Usage: test EXPRESSION
  or:  test
  or:  [ EXPRESSION ]
  or:  [ ]
  or:  [ OPTION
Exit with the status determined by EXPRESSION.
[...]

In the bash builtin version, the only difference is that [ requires ] at the end, as you said.

Related Question