if expr statement (without brackets) checks the return status of
expr after it's evaluated as a bash command. Upon success, it is treated as a
On the other hand,
if [[ expr ]] with double square brackets is a
bash-specific keyword that can handle its own set of options, such as
-f /path/to/file to test for file existence, or even handle variable tests such as
$a == $b. Within these brackets, you have to explicitly tell it to evaluate some expression as a set of bash commands with
if [[ $(cmds) ]]. Otherwise, it will treat anything there as a special expression and will be parsed differently.
There's also the
if [ expr ] construct, which is generally an alias (or, in some cases, an approximative implementation of)
if test expr. If the
test command has an option available to use, then you can also use it between single brackets.
For more fun, there's the
if ((expr)) construct, which is best when dealing with numbers, since everything inside is evaluated as an arithmetic expression. This is also a keyword, like the double brackets.
if expr and
if [ expr ] are portable (i.e. all shells can handle them, and handle them pretty much identically) and
if [[ expr ]] and
if (( expr )) are implementation-specific, i.e. they are keywords whose evaluation depend highly on the shell you're using them in (and if you're not using something like
ksh, they might not even exist).