double-bracket
enhanced conditional expression for bash/zsh
TLDR
SYNOPSIS
[[ expression ]]
DESCRIPTION
[[ is bash/zsh's enhanced conditional expression. It provides safer and more powerful tests than [.Key advantages over [:- No word splitting on variables (unquoted $var is safe)- Pattern matching with == and !=- Regular expression matching with =~- && and || work inside the brackets- < and > don't need escaping
[[ $file == *.txt ]] && echo "text file"
# Regex matching
if [[ $email =~ ^[A-Za-z]+@[A-Za-z]+\.[A-Za-z]+$ ]]; then
echo "Valid email format"
fi
# No quoting needed
[[ $var == "test" ]] # Safe even if var is empty
OPERATORS
File tests: Same as [ (-f, -d, -e, -r, -w, -x, etc.)String comparison:- == or =: Pattern match (glob)- !=: Not pattern match- =~: Regex match- <, >: Lexicographic orderNumeric: -eq, -ne, -lt, -le, -gt, -geLogical:- &&: AND- ||: OR- !: NOT
CAVEATS
[[ is bash/zsh specific, not POSIX. Use [ for portable scripts.Regex with =~ uses ERE (Extended Regular Expressions). Captured groups go in BASH_REMATCH array.Quote the right side of == to match literally: **[[ $x == "pattern*" ]]** matches the literal string.Don't quote regex patterns: [[ $x =~ ^test ]] not [[ $x =~ "^test" ]]
