Bash double brackets and patterns
[[ expression ]] is a Bash conditional construct, not an ordinary command with bracket arguments. Inside it, variable expansions do not undergo word splitting or pathname expansion. [[ $name == *.txt ]] matches a shell pattern; [[ $name == "*.txt" ]] compares with the literal characters *.txt. A variable used as a pattern has its pattern characters active unless quoted. Use [[ $left == "$right" ]] for literal comparison. [[ ]] supports !, &&, || and grouping with parentheses; inside this construct AND has higher precedence than OR. String ordering with < and > depends on locale in [[ ]]. For portable scripts use appropriate POSIX test forms instead. These examples assume default options, including case-sensitive matching. With shopt -s nocasematch, even quoted literal comparisons can ignore case; quoting disables pattern metacharacters, not the case-insensitive option.
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory. Tests use default shell options and C locale. Output is shown separately; do not enter it as code.
Match a filename pattern
name='daily report.txt'if [[ $name == *.txt ]]; then printf '%s\n' text; fiExpected stdout:
textThe wildcard matches the prefix including the space.
Literal or pattern
name=report.txtpattern='*.txt'[[ $name == $pattern ]]; printf '%s\n' "$?"[[ $name == "$pattern" ]]; printf '%s\n' "$?"Expected stdout:
01The unquoted right side is a pattern; the quoted value is literal.
Group conditions
role=readermode=activeif [[ ( $role == reader || $role == editor ) && $mode == active ]]; then printf '%s\n' allowedfiExpected stdout:
allowedThe role group must match and mode must be active.
Common mistakes
Quoting the right side is a semantic choice: it disables pattern meaning in the quoted portion. Do not use an unquoted user-supplied value as a pattern if you intend literal equality.
Practice
Classify budget.csv using *.csv. Then compare it with a variable holding the literal text *.csv, both quoted and unquoted.
Next: Regex matching with =~