Using [[ ]] for Bash Conditionals
Lesson 68 showed that [ ] is an ordinary command, and that its quirks come from that fact: unquoted variables can vanish or split, < needs escaping, and combining tests is awkward. Bash offers a second form, the double-bracket [[ ]], designed to remove those problems. It is not a command. It is part of the shell's own syntax, so Bash can read the whole expression before expanding anything, and that changes how it behaves.
Understanding the Concept
[[ expression ]] is a compound command that evaluates a conditional expression and returns an exit status, just like [ ] does. It can be used anywhere a condition is accepted:
if [[ $name == "Ana" ]]; then
echo "Hello, Ana"
fi
Because [[ is a keyword rather than a command, you can confirm the difference with type:
type [[
Output:
[[ is a shell keyword
Compare that with [ is a shell builtin from lesson 68. A keyword is recognized while Bash is still parsing the line, before it performs word splitting or filename expansion. This is what makes [[ ]] safer and more expressive.
Syntax
[[ EXPRESSION ]]
As with [ ], spaces are required after [[ and before ]], and around operators. The expression can use all the string, integer and file operators from lesson 68, plus several that exist only in [[ ]]:
- pattern matching with
==and!=against glob patterns, - regular expression matching with
=~, <and>string comparison without escaping,&&,||and( )for combining tests inside one expression.
No Quoting Problems
The most visible difference is what happens to variables. Inside [[ ]], an unquoted variable is not word-split and an empty variable does not vanish:
empty=""
[[ $empty = "x" ]]; echo "empty unquoted =: $?"
[[ -n $empty ]]; echo "empty unquoted -n: $?"
v="two words"
[[ $v = "two words" ]]; echo "spaces: $?"
Output:
empty unquoted =: 1
empty unquoted -n: 1
spaces: 0
In lesson 68, the equivalent [ ] tests produced errors or the wrong answer. Here all three give the correct result: an empty string is not equal to x, -n correctly reports an empty variable as empty, and a value with a space stays in one piece.
You may still quote variables out of habit, and doing so does no harm. Quoting matters on the right side of == and =~, as the next sections show.
Pattern Matching with ==
Inside [[ ]], the right side of == and != is a glob pattern (the same wildcard syntax used for filenames, covered in lesson 52). An unquoted pattern matches by wildcard rules; a quoted pattern is compared literally.
file="report.txt"
[[ $file == *.txt ]]; echo "*.txt: $?"
[[ $file == r* ]]; echo "r*: $?"
[[ $file == *.pdf ]]; echo "*.pdf: $?"
[[ $file == "*.txt" ]]; echo "quoted pattern literal: $?"
[[ $file == ??????.txt ]]; echo "??????.txt: $?"
[[ $file == [rs]*.txt ]]; echo "[rs]*.txt: $?"
Output:
*.txt: 0
r*: 0
*.pdf: 1
quoted pattern literal: 1
??????.txt: 0
[rs]*.txt: 0
The wildcards used are * (any run of characters), ? (exactly one character), and [rs] (one character from a set). The test "*.txt" in quotes asks whether the text is literally an asterisk followed by .txt, and report.txt is not that, so the result is false.
Matching happens against the value of the variable only. The pattern is never expanded against files in the current directory, so even if files that match *.txt exist, they have no effect:
touch one.txt two.txt
pat="*.txt"
[[ $pat == "*.txt" ]]; echo "literal star: $?"
Output:
literal star: 0
The single = works the same as == inside [[ ]], and != negates the match:
[[ a = a ]]; echo "= : $?"
[[ a == a ]]; echo "== : $?"
[[ a != b ]]; echo "!= : $?"
Output:
= : 0
== : 0
!= : 0
Case Sensitivity
Matching is case-sensitive by default. The nocasematch shell option turns that off for pattern and regex matching in [[ ]]:
[[ HELLO == hello ]]; echo "case differs: $?"
shopt -s nocasematch
[[ HELLO == hello ]]; echo "nocasematch: $?"
shopt -u nocasematch
Output:
case differs: 1
nocasematch: 0
The shopt -s command switches an option on and shopt -u switches it off. Lesson 149 covers shopt. Turning an option off again afterward keeps the rest of the script predictable.
Regular Expression Matching with =~
The =~ operator matches the left side against an extended regular expression, which is a more powerful pattern language than globs. It is true if the expression matches anywhere in the text, so anchors ^ and $ are used to demand a match of the whole value.
email="ana@example.com"
if [[ $email =~ ^[a-z]+@[a-z]+\.[a-z]+$ ]]; then
echo "looks like email"
fi
Output:
looks like email
The pattern reads: start of text, one or more lowercase letters, an @, one or more letters, a literal dot (escaped as