Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

Bash Manual says:

When the [ form is used, the last argument to the command must be a ].

$ type [
[ is a shell builtin
$ type ]
bash: type: ]: not found

So ] isn't a reserved word, nor is it an operator, nor is it a builtin command.

As a token, what is the token identifier of ]? WORD or NAME?

share|improve this question
4  
Actually, [ is (also) a self-sustained command: -rwxr-xr-x 1 root root 39552 Dec 3 18:14 /usr/bin/[ Though it's possible there is also "parallel" built-in, like there is for for example kill. On some system, the test command is a symbolic link to [ or vice versa. AFAIK, ] doesn't mean anything by itself, but is accepted to "close" a [ - but it's more for "aesthetic" reason. – Baard Kopperud Mar 16 at 10:31
5  
The shell language has grown layer by layer over the past 46 years, and internal consistency was never a priority. If you try to force-fit it into any sort of theoretical formal-language framework you are going to have a bad time. – zwol Mar 16 at 13:01
    
Since [ is (also) a self-contained command, it usually got a man-page... So try man [ and/or man test ([ and test may be the same command). – Baard Kopperud Mar 16 at 14:37
    
Try sticking the ] in single or double quotes and you will see it makes no difference; it's just a string. – Wildcard Mar 17 at 17:21

] complements [, it is the closing sign of [ command.

As the man page points out, this is actually an argument to [, but [ happens to treat it especially, as the ending.

You can resemble it with some other command closing patterns, for example ; in find .. exec.

share|improve this answer
    
Thanks. What is the token type/identifier of ]? – Tim Mar 16 at 9:12
8  
@Tim ] is no different from any other normal character in this context (as an argument to a command). The test command treats it specially, but that's up to the command. – muru Mar 16 at 9:36
6  
It's just a string, and [ raises an error if its final argument isn't exactly equal to ]. – chepner Mar 16 at 12:55

Bash does not treat the [ or ] characters (on their own) any different from a letter of the alphabet.

In this case bash sees a "word", [, and goes looking for a command with that name. If you look in your filesystem you'll probably find that /bin/[ (or /usr/bin/[) exists as an executable file. As it happens, bash also provides a built in version (for the sake of efficiency), but that's just an implementation detail.

The [ command (as in, the executable with that name), has a rule that the last parameter you pass to it should be ] or else it throws an error. This may be partly because it's aesthetically pleasing, but it also serves to protect you from accidentally truncated commands, which is nice.

The fact that these are not special is why you can't omit the white space before and after the [ or ].


Exceptions:

  • When [ and ] occur in the correct format within the same word, e.g. [a-z], then that is special and bash's globbing rules apply.
  • The [[ command is special and can do many things [ cannot (and parameters inside [[ .. ]] are processed differently, as are some line breaks). The corresponding ]] is also special, as it is a shell reserved word that cannot be a command name, and terminates the special processing that follows the [[ keyword.
share|improve this answer
1  
@JacobKrall It's not a special character like ; or >, it's just a built-in command like cd. In particular, if you type [x, the [ has no special meaning. – Barmar Mar 16 at 18:19
2  
@JacobKrall That's true for all built-in commands. [ is a special command, it's not a special character. – Barmar Mar 16 at 18:27
    
@Barmar: Fair enough; I retract my comments. – Jacob Krall Mar 16 at 18:28
    
Actually ]] is more special than ]. When using [ and ] they are just words. If you omitted ] or replaced it with a different word, bash will still run [, but [ will produce an error. However [[ and ]] are treated special at parsing time. If you replaced ]] with something else, bash will report a syntax error. And if you omitted ]] bash would keep parsing the next line looking for the ]]. – kasperd Mar 17 at 12:37
    
@kasperd answer adjusted. – ams Mar 17 at 17:01

To put it more simply than the previous two answers, ] is just a string that [ requires to run.

share|improve this answer
1  
This seems a bit like a comment... – wizzwizz4 Mar 16 at 17:44
    
@wizzwizz4, given the followup questions from the OP in comments, I think this simplicity is exactly what he needed. – Wildcard Mar 17 at 17:16
    
@Wildcard I actually agree. There was something tempting me to upvote this answer, but I thought it was low quality. (Didn't downvote though!) I'm going to edit to improve the markup. – wizzwizz4 Mar 17 at 17:19

Your Answer

 
discard

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.