Shell syntax, as with most programming, tends to be very exact and sensitive to error. Various characters are assigned very specific meanings and can generally accept very little deviation. When you get an example form like the above, try to follow it to the letter...at least until you fully understand what can and can't be done with it.
Incidentally, while [ .. ]
is the traditional bourne-compatible test command, supported by all posix shells, bash and ksh also provide a newer test: the [[ .. ]]
, the expanded test keyword, which provides some additional features and corrects some of the flaws of the original.
There's also (( .. ))
, the arithmetic evaluation brackets. Since they also output a true/false exit status, they can be used in place of the square bracket tests when comparing integer values.
Unless you really need portability to old, non-posix shells, it's generally a good idea to use the [[..]] test for all of your string evaluations, and ((..)) for numerical tests.