Test Commands: [ ], [[ ]], and test

Updated

October 31, 2026

Test Commands: [ ], [[ ]], and test

Conditional branching in Bash relies on test evaluation. Understanding the difference between [ ] (the test builtin) and [[ ]] (the Bash compound keyword) is essential.

Single Bracket [ ] (POSIX standard)

[ is literally a command (identical to test), requiring closing bracket ]. Because it is a normal command, arguments are subject to word splitting and pathname expansion:

# Must quote all variables to prevent syntax errors on empty strings
if [ "$name" = "root" ]; then
    echo "Superuser"
fi

Double Bracket [[ ]] (Bash Keyword)

[[ is a shell keyword with special grammar rules: * No word splitting or pathname expansion on variable expansions inside [[ ]]. * Supports regular expression matching with =~. * Logical operators && and || work natively without -a or -o. * Pattern matching with == and !=.

# Regex validation
email="dev@kanthi.in"
if [[ $email =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    echo "Valid email address"
fi