Bash Scripting
Coding Standards
Programming
Linux
Syntax Tips

Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Bash scripting, the choice between single square brackets [ ] and double square brackets [[ ]] often confuses beginners. However, understanding their differences is crucial for writing robust and efficient scripts. This article explores the advantages of using double square brackets over single square brackets, with technical explanations and examples.

Basic Differences

The [ ] is essentially a synonym for the test command. It has been a part of UNIX since its early days. The [[ ]], on the other hand, is a more recent Bash construct that offers enhanced features, which facilitate more intuitive and safer scripts.

Syntax Flexibility

Double square brackets [[ ]] provide greater flexibility in scripting syntax. They allow the use of logical operators (&&, ||) directly within them, which is not supported with single square brackets.

Example:

bash
1# Using single square brackets
2if [ "$a" = "foo" ] && [ "$b" = "bar" ]; then
3  echo "Condition met."
4fi
5
6# Using double square brackets
7if [[ "$a" == "foo" && "$b" == "bar" ]]; then
8  echo "Condition met."
9fi

Pattern Matching

The [[ ]] supports pattern matching using the =~ operator, which does not work within [ ].

Example:

bash
1# Checking regex match
2if [[ $input =~ ^[0-9]+$ ]]; then
3  echo "Contains only digits."
4else
5  echo "Contains non-digit characters."
6fi

Improved String Comparisons

With [[ ]], the == operator is used for pattern matching. It treats the right-hand side as a pattern and matches it against the left-hand side as a string.

Example:

bash
1string="MyExample"
2if [[ $string == My* ]]; then
3  echo "String starts with 'My'."
4fi

In contrast, using [ ] with == leads to a more complex and error-prone expression:

bash
if echo "$string" | grep "^My" > /dev/null; then
  echo "String starts with 'My'."
fi

Null and Unset Variables

The [[ ]] test is safer with unset or null variables. Unlike [ ], it does not require quotes around variables to prevent script-breaking errors when a variable is unset or null.

Example:

bash
1unset variable
2# No need to quote the variables
3if [[ $variable == "value" ]]; then
4  echo "This won't cause an error even if variable is unset."
5fi

Table of Differences:

Feature[ ][[ ]]
Syntax flexibilityLimitedHigh
Use of logical operatorsExternalInternal
Pattern matchingNot supportedSupported with == and =~
Null and unset variable handlingError-proneSafe
Regex matchingNot applicableSupported with =~

Conclusion

While both [ ] and [[ ]] are used for testing expressions in Bash, the [[ ]] is clearly superior in terms of functionality and safety. It provides advanced features that make scripts more readable and less prone to errors, especially in complex conditions with string operations and pattern matching. For modern Bash scripts, [[ ]] is generally preferable, unless you are aiming for strict POSIX compatibility, in which case [ ] must be used.


Course illustration
Course illustration

All Rights Reserved.