Difference Between Single and Double Square Brackets in Bash

Fumbani Banda Jan 30, 2023
  1. Single Square Brackets [ ] in Bash
  2. Double Square Brackets [[ ]] in Bash
  3. Differences Between Single and Double Square Brackets in Bash
Difference Between Single and Double Square Brackets in Bash

This tutorial explains what single square brackets and double square brackets are in Bash and their differences.

Single Square Brackets [ ] in Bash

A single square bracket [, is another name for the test command in Bash shell. The test command is a standard utility in all POSIX shells. The ] is the last argument for [.

The two scripts below show that [ and the test command is similar. The first script uses the single square brackets to check if the value in the variable $y is greater than the value in the variable $x and prints the output depending on the results returned from the evaluation in the test command.

In our case, the value of the $y variable is great than the value of the $x variable.

#!/bin/bash

x=2
y=3

if [ $y -gt $x ]
then
    echo "$y is greater than $x"
else
    echo "$x is greater than $y"
fi

The script prints the following message to the standard output when executed.

3 is greater than 2

This script uses the test command instead of the single square brackets. The script checks if the value in the $y variable is greater than the value in the $x variable.

If the test returns true, the script executes the first echo command, and if the test returns false, it executes the echo command in the else section.

In our case, the $y variable has a value of 3 greater than the $x variable, 2. The script will execute the first echo command.

#!/bin/bash

x=2
y=3

if test $y -gt $x
then
    echo "$y is greater than $x"
else
    echo "$x is greater than $y"
fi

The script prints the following output to the standard output.

3 is greater than 2

Double Square Brackets [[ ]] in Bash

The double square brackets [[]] extend the test command adopted from ksh88; it is more versatile. The double square brackets can be used for pattern matching, parameter expansion, and they do not allow word splitting.

Using double square brackets helps to avoid logic errors in Bash scripts. In the double square brackets, the &&, ||, < and > operators work while giving errors in the test command.

We use double square brackets in the script below for arithmetic evaluation. The double square brackets test if the value in the $x variable equals the value in the $y variable.

The test returns true and executes the first echo command in the script.

#!/bin/bash

x=10
y=10

if [[ $x -eq $y ]]
then
    echo "\$x is equal to \$y"
else
    echo "\$x is not equal to \$y"
fi

Running the script produces the following output to the standard terminal.

$x is equal to $y

Differences Between Single and Double Square Brackets in Bash

The test command is a built-in Bash utility on standard POSIX shells, while the double square brackets are not a command. The double square bracket is an extension in Bash adapted from ksh88 used as a keyword.

The double square brackets support more features compared to the test command. Unlike the test command, it supports pattern matching and parameter expansion and does not allow word splitting.

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub