The single square brackets ([ ... ]
) is an synonym of the test
command. If you look at the man page for test, you will see almost all (Bash might have a few extra not mentioned here) of the various if switches as you called them. All in one easy-to-find place.
If you use double square brackets ([[ ... ]]
), you are using an extended Bash set of tests. These mainly have to do with regular expression matching, and glob matching (and extended glob matching if you have that set too). For that, you'll have to read that Bash man page.
You called them if switches, but that's not really correct. These are tests and really have nothing to do with the if
command.
The if
command merely executes the command you give it, and then if that command returns an exit code of 0
, will run the if
portion of the if
statement. Otherwise, it will run the else
portion (if that's present).
Let's look at this:
rm foo.test.txt # Hope this wasn't an important file
if ls foo.test.txt
> then
> echo "This file exists"
> else
> echo "I can't find it anywhere.."
> fi
ls: foo.test.txt: No such file or directory
I can't find it anywhere..
The if
statement runs the ls foo.test.txt
command and the ls
command returns a non-zero because the file does not exist. This causes the if
statement to execute the else
clause.
Let's try that again...
touch foo.test.txt # Now this file exists.
if ls foo.test.txt # Same "if/else" statement as above
> then
> echo "This file exists"
> else
> echo "I can't find it anywhere.."
> fi
foo.test.txt
This file exists
Here, the ls
command returned a 0
exit status (since the file exists and the file exists and can be stat'ed by the ls
command.
Normally, you shouldn't use the ls
command to test for a file. I merely used it here to show that the if
statement executes the command, then executed the if
or else
clause depending upon the exit status of that command. If you want to test whether or not a file exists, you should use the test -e
command instead of ls
command:
if test -e foo.test.txt # The same as above, but using "test" instead of "ls"
then
echo "This file exists"
else
echo "I can't find it anywhere..."
fi
If the file exists, test -e
will return an exit status of 0
. Otherwise, it will return a non-zero exit status.
If you do this:
ls -i /bin/test /bin/[
10958 /bin/[ 10958 /bin/test
That 10958
is the inode. Files with the same inode are two different names for the same file. Thus [
and test
command are soft linked1. This means you can use [
instead of test
:
if [ -e foo.test.txt ]
then
echo "This file exists"
else
echo "I can't find it anywhere.."
fi
Does it look familiar?
1. In Bash, the test
and [
are builtin, so when you run these commands in BASH, it isn't running /bin/test
or /bin/[
. However, they're still linked to each other.