Frequently Asked Question

What is the difference between [ ] and [[ ]] in bash?

Single brackets [ ... ] are the original POSIX test, and they are literally a command called test (or its alias [, which expects a closing ] as its last argument). Because it is a regular command, every word inside gets the usual shell treatment: word splitting and globbing happen, so an unquoted variable that is empty becomes nothing and breaks the syntax. You have to write [ "$x" = "foo" ] with the quotes, and you can only combine tests with -a and -o, which are deprecated and unreliable.

Double brackets [[ ... ]] are a bash keyword, parsed by the shell itself rather than executed as a command. That means no word splitting, no globbing of bare variables, and proper short-circuit && and || inside the brackets. They also add pattern matching ([[ $name == A* ]]`) and regular expression matching (`[[ $name =~ ^[A-Z][a-z]+$ ]]). In bash scripts use [[ ... ]]; reserve [ ... ] for scripts that must be POSIX sh portable, such as init scripts that may run under dash or busybox.

Video

Further reading and video