Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Regex matching in Bash (=~)

Reference for regular expression matching in Bash: the =~ operator inside [[ ]], extended regex syntax, capture groups in BASH_REMATCH, quoting rules and pitfalls.

Bash can match a string against a regular expression without calling grep or sed. The operator =~ inside a [[ ... ]] test treats its right-hand side as a POSIX extended regular expression (ERE). If the string matches, the test succeeds, and Bash stores the whole match and every parenthesized group in the array BASH_REMATCH.

This is the cheapest way to validate input or pull a few fields out of one line, because no extra process is started. For anything line-by-line over a big file, grep or awk is still the better tool.

Terminal
[[ STRING =~ REGEX ]]
echo "${BASH_REMATCH[0]}"    # whole match
echo "${BASH_REMATCH[1]}"    # first (...) group

Try it

Bash Validate and capture

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 Storing the pattern in a variable

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
[[ string =~ pattern ]]
Name Description
string The text to search. It is not word-split, so quoting is optional.
pattern An extended regular expression. The match is unanchored: it succeeds if the pattern matches anywhere. Add ^ and $ to anchor.
BASH_REMATCH[0] The part of the string that matched the whole pattern.
BASH_REMATCH[n] The text matched by the n-th parenthesized group; empty if that group did not take part.
BASH_REMATCH after failure Empty (no elements).

Returns: 0 for a match, 1 for no match, 2 if the regular expression is syntactically invalid.

Errors and edge cases: the regex flavor is whatever the C library's regcomp supports, so there are no lazy quantifiers (.*?), no lookahead and no \d. Use [0-9] and [[:digit:]] instead.

Usage notes

  • Quoting rules changed once. In Bash 3.1 a quoted pattern was still treated as a regex; from 3.2 on, quoted parts are literal text and unquoted parts are regex. The portable habit is to store the pattern in a variable and use it unquoted: [[ $x =~ $re ]].
  • Spaces and special characters. An unquoted space ends the pattern, so a pattern with spaces must be quoted or put in a variable. Write [[:space:]] instead of a literal space when you can.
  • Parentheses and |. In a pattern given directly inside [[ ]], bare ( ) | are accepted, unlike in the rest of the shell, but quoting them would make them literal. A variable avoids any doubt.
  • Capturing repeated groups. A group inside a quantifier, such as (a|b)+, keeps only the last repetition in BASH_REMATCH[1].
  • Position info. BASH_REMATCH has no offsets. To find where something matched, strip the prefix from the string yourself, as in the example above.
  • Not for finding every match. There is no global flag, so loop and consume the string, or use grep -o.
  • nocasematch (shopt -s nocasematch) makes =~, == in [[ ]] and case ignore case; remember to turn it off again.
  • The array is global. The next =~ overwrites it, so copy values into named variables right away if you need them later.