Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash [[ ]] conditional expressions

Reference for Bash [[ ]] conditions: glob pattern matching with ==, regular expressions with =~ and BASH_REMATCH, && and || inside, and differences from [ ].

[[ expression ]] is Bash's extended test. It accepts all the test operators of [ ] but is parsed by the shell itself, so variables are not word-split or glob-expanded, &&, || and parentheses work inside, and the right side of == is a glob pattern. With =~ it also matches regular expressions and captures groups.

Because it is shell syntax rather than a command, [[ ]] is safer and more capable than [ ] in Bash scripts, but it is not available in plain POSIX sh.

Terminal
[[ $name == a* ]]           # glob match
[[ $text =~ ^[0-9]+$ ]]     # regex match
[[ -f $f && -r $f ]]        # combined tests

Try it

Bash Glob patterns and logic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Regular expressions and BASH_REMATCH

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
[[ expression ]]
Form Description
[[ -e file ]] and the other file/string/number tests Same meaning as in [ ].
[[ str == pattern ]], [[ str = pattern ]] True if str matches the glob pattern (*, ?, [...], and extended patterns with shopt -s extglob).
[[ str != pattern ]] True if it does not match.
[[ str =~ regex ]] True if str matches the POSIX extended regular expression; sets BASH_REMATCH.
[[ a < b ]], [[ a > b ]] String comparison in the current locale; no escaping needed.
[[ e1 && e2 ]], [[ e1 || e2 ]], [[ ! e ]], [[ ( e ) ]] Logic and grouping with short-circuit evaluation.
[[ -v name ]] True if the variable is set.

Returns: 0 if the expression is true, 1 if false, 2 if the regular expression is invalid.

Usage notes

  • Quote the right side to force a literal comparison: [[ $x == "a*" ]] matches only the text a*, while [[ $x == a* ]] is a pattern. The same rule applies to =~: a quoted part of a regex is matched literally.
  • For regular expressions with spaces or special characters, store the regex in a variable and use it unquoted ([[ $s =~ $re ]]). This is the most portable form across Bash versions.
  • BASH_REMATCH[0] is the whole match and [1], [2]... are the capture groups. The array is overwritten on every successful =~, and it is not reset by a failed match in some situations, so test the result before reading it.
  • =~ uses extended regex of the C library: no \d, \w or non-greedy *?. Write [0-9] and [[:alpha:]]. Behavior of \b and \s depends on the platform.
  • Word splitting and globbing are off for the unquoted variables inside, so [[ -n $v ]] works even when $v is empty or has spaces.
  • < and > compare strings; for numbers use -lt and -gt, or (( )). Inside [[ ]], == with numbers is still a string comparison.
  • [[ ]] is a keyword, so [[ -f $f ]] cannot be wrapped in "$@" like a command. For POSIX scripts use [ ].
  • shopt -s nocasematch makes ==, !=, =~ and case ignore case.