Double Square Brackets [[ ]]
The [[ and ]] are used for conditional expressions in Bash. The [[ is an advanced version of the single square brackets [ ] and is used for pattern matching, regular expressions, and advanced conditional expressions. The [[ operator is more powerful than [ and is preferred for many cases.
if [[ $string == "Linux" ]]; then
echo "The string is Linux"
In the following code, the double square brackets operator is used to check if the value of the variable $string is equal to the string “linux”:
Single Square Brackets [ ]
The [ and ] are used for conditional expressions in Bash. It is also called the “test” command. The single square brackets operator [ is also used for conditional expressions but has more limitations than [[. It is useful for basic comparisons and tests, such as checking if a file exists or if a string is empty.
# Define the name of the file to check
# Check if the file exists
if [ -f $file ]; then
echo "The file exists"
echo "The file does not exist"
This script checks if a file named “bashfile2.sh” exists in the respective directory and if the file exists, it prints the message “The file exists” to the console else it will print “The file does not exist”:
Parentheses ( )
The parentheses operator ( is used to run commands in a subshell environment. This means that any changes made to the environment within the parentheses do not affect the parent shell environment.
# Change directory to /home/user/directory and list its contents
(cd /home/aaliyan/Documents && ls)
In the following code, the parentheses operator is used to change the directory to /home/aaliyan/Documents and run the ls command within a subshell:
Double Parentheses (( ))
The (( and )) are used for arithmetic evaluation and It supports more advanced features than the single parentheses operator (, such as bitwise operations, below is an example bash script that demonstrates the use of double parentheses:
# Define the values of a, b, and c
# Check if the sum of a and b is greater than c
if (( $a + $b > $c )); then
echo "The sum of a and b is greater than c"
In the following code, the double parentheses operator is used to check if the sum of the variables $a and $b is greater than the value of the variable $c:
To further Understand the differences between these operators I have given a table that can help you write more efficient bash code and use these bash operators more effectively:
|[[ expression ]]||Conditional expressions||if [[ $string == “linux” ]]; then echo “The string is linux”|
|[ expression ]||Basic Conditional expressions||if [ -f $file ]; then echo “The file exists”|
|( command )||Run commands in a subshell environment||(cd /home/user/directory && ls)|
|(( expression ))||Arithmetic operations||if (( $a + $b > $c )); then echo “The sum of a and b is greater than c”|
The Bash operators [[ ]], [ ], ( ), and (( )) have different uses and syntax. The double square brackets [[ ]] operator is used for pattern matching and string comparison, while the single square brackets [ ] operator is used for conditional statements and file tests. The parentheses ( ) operator is used to group commands together, and the double parentheses (( )) operator is used for arithmetic evaluation. It’s important to use the appropriate operator for the task at hand to ensure that your Bash scripts are accurate and efficient.