8

Examining man test, I see that under synopsis for test are the possibilities test EXPRESSION and test.

What does [ EXPRESSION ], [ ] and [OPTION mean below ?

Why are the brackets empty and why does [OPTION miss a bracket ? Can someone interpret this for me ?

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       test

       [ EXPRESSION ]
       [ ]
       [ OPTION
10

[ is another name for test. All three of those lines are command lines that run test with some options.

In the first line, this is standard testing: [ 5 -gt 4 ] is the same as test 5 -gt 4.

In the second, the expression is omitted, which means to exit false (0 arguments: Exit false (1)).

For the third case you are, I guess, using GNU coreutils. In GNU test the help text contains this note:

NOTE: [ honors the --help and --version options, but test does not.
test treats each of those as it treats any other nonempty STRING.

This is a non-POSIX extension; the motivation seems to be that test is required to treat those arguments as strings like any other. [ is able to distinguish the option case from the string case by the presence of the closing ] bracket.

Note that your shell will likely provide its own [, and so you'll have to /bin/\[ to use this version.

4

[ is the same as test as a shell builtin on some shells.

If you look further down man test, it describes what are the valid EXPRESSION you can test with:

An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of: ...

This may not be apparent to you from the man-page, but in older/some other shells, both [ and test are available as actual commands, which means the space you see in [ EXPRESSION ] is mandatory for EXPRESSION to be accepted as an argument for [/test. Thus, even for shells which offer them as a builtin, they retain this format for compatibility.

And as explained in @dr01's answer while I'm typing this, the right square bracket is optional.

  • @don_crissti thanks for the heads-up, I think I'll just simply omit that last sentence... – h.j.k. Aug 21 '15 at 10:36
0

[ OPTION is not really missing a bracket.

In fact, perhaps surprisingly, [ is a command (located in /usr/bin/[) equivalent to test. Therefore it was (at least theoretically) permitted to use just the left bracket, with the right bracket serving just as syntactic sugar. However, newer shell versions require it.

  • Are there other differences between these than test cannot be used with an option (not listed with that possibility). – Shuzheng Aug 21 '15 at 9:49
  • 1
    It is not permitted to omit the right bracket, either in POSIX or GNU [. – Michael Homer Aug 21 '15 at 9:53
  • @MichaelHomer Yes. Corrected. – dr01 Aug 21 '15 at 9:57
  • I'm not sure what Bash has to do with this... – Michael Homer Aug 21 '15 at 9:57
  • 1
    @MichaelHomer, in POSIX, the behaviour if you omit the right bracket is not specified, so a POSIX script is not permitted to omit the ], but a POSIX [ may do what it wants including calling you names and reboot the machine or print a help message if you call it without it. That allows GNU grep's [ --version. test --version and [ --version ] however are required to return true (test for --version being the non-empty string) and output nothing. – Stéphane Chazelas Aug 21 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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