David the H. |
11-23-2011 10:26 AM |
The link given gives you all you need to know. But to boil it down:
[...] The traditional test command, [ is actually a synonym for " test". Portable across shells and systems, but suffers from syntactical weaknesses.
[[...]] The "new" test keyword. First implemented in ksh, and also adopted by bash. It overcomes a lot of the problems with the old test, and adds some new features like pattern-matching with globs and regex. Everything that the original does, this one can do too, and often better. But it's not portable.
((...)) Arithmetic evaluation. Supported in both bash and ksh these days. Since it outputs an exit code, it can be used in place of one of the above tests when the operation involves numbers. But remember, the shell does integer math only.
So in a nutshell, use ((...)) for arithmetic comparisons and [[...]] for string and complex comparisons. Only use [...] if you truly need posix-style compatibility, which you probably don't. But do learn how to use it, for those times when you come across it.
Edit: by the way, be very careful to recognize the difference between a string and a numeric comparison. Try these two commands, for example:
Code:
[[ 99 > 100 ]] && echo "dat's true" || echo "say what?"
[[ 99 -gt 100 ]] && echo "dat's true" || echo "say what?"
|