Even since I started writing Bash script, I always use [[ for if conditional statement, the double square brackets, never the single one version unless compatibility has to be taken into account. Why did I choose it? It’s because I read [[ is faster. I’ve never tried to confirm it by myself, although I do know that [ is a type of shell builtin command and [[ is type of shell keyword, and [ is equivalent to test, which is also a builtin command in Bash for very long time.1

Note

[ and [[ can also be used for arithmetic comparison, such as the use of -gt operator, see another test with Arithmetic Evaluation, ((. This post only focuses on the strings.

From time to time, I often see people still using [ even in the script with a few Bash-only syntaxes or features. They are clearly not writing with compatibility as a requirement. If I have a chance, then I would probably advise the coder to change to [[. I had done so a few times in the past.

However, I never see the numbers, so I used the following to test:


time for ((i = 0; i < 10000; i++)); do <test> -z '' ; done

The result is:

<test> withtime% slower
[00.149s00,063.7%
[[00.091sfastest
test (builtin)00.150s00,064.8%
/usr/bin/test13.798s15,063.6%

[ and test possibly are synonyms since they are pretty close.

As you can see [[ definitely is the winner, and /usr/bin/test external command is the slowest. The problem with /usr/bin/test isn’t that is inefficient, but external command is costly.

If you are new to Bash, just use [[.

[1]If you don’t know the differences between keyword, builtin command, and external command, google them.