> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash Regex Match

The `=~` operator is a binary regular expression matching operator used exclusively within Bash's extended conditional expression construct (`[[ ]]`). It evaluates whether a string on the left side matches an Extended Regular Expression (ERE) provided on the right side.

```bash theme={"dark"}
[[ $string =~ $regex ]]
```

## Execution Mechanics

* **Engine:** Bash utilizes the system's C library regex functions (`regcomp` and `regexec`) to evaluate the pattern as a POSIX ERE. Because regex parsing is delegated to the underlying system library, the availability of non-standard extensions varies. On systems using glibc (most Linux distributions), shorthand character classes like `\s` and `\w` are supported as ERE extensions. However, for strict cross-platform portability, standard POSIX character classes like `[[:space:]]` or `[[:digit:]]` are preferred.
* **Exit Status:** The operator yields an exit status of `0` (true) if the string matches the pattern, `1` (false) if it does not match, and `2` if the regular expression is syntactically invalid.
* **Case Sensitivity:** By default, the regex evaluation is case-sensitive. This behavior can be altered globally by enabling the `nocasematch` shell option (`shopt -s nocasematch`), which instructs the `=~` operator to evaluate patterns case-insensitively.
* **Compatibility:** This operator is a Bash extension and is not POSIX-compliant. It will result in a syntax error if used within the standard `[ ]` (test) command.

## Quoting and Evaluation Rules

The parsing of the right-hand side of the `=~` operator is highly sensitive to quoting.

If any part of the regular expression is enclosed in single (`'`) or double (`"`) quotes, Bash treats that specific quoted portion as a **literal string** rather than a regex metacharacter.

```bash theme={"dark"}

# Evaluates as a regular expression (matches "a", "b", or "c")
[[ $string =~ [abc] ]]


# Evaluates as a literal string (matches the exact sequence "[abc]")
[[ $string =~ "[abc]" ]]
```

To avoid parsing errors with complex patterns containing spaces or shell metacharacters (like `|`, `<`, `>`), the standard technical practice is to assign the regular expression to a variable before evaluation.

```bash theme={"dark"}

# Recommended syntax using POSIX ERE character classes
pattern='^prefix-([0-9]{3})[[:space:]]+(.*)$'
[[ $string =~ $pattern ]]
```

## Capture Groups and `BASH_REMATCH`

When a successful match occurs, Bash automatically populates a global array variable named `BASH_REMATCH` with the results of the match and any parenthesized subexpressions (capture groups). This array is strictly **read-only** and managed exclusively by the shell. Attempting to modify, reassign, or unset it will result in a runtime error (e.g., `bash: BASH_REMATCH: readonly variable`).

* **`${BASH_REMATCH[0]}`**: Contains the contiguous substring of the left-hand string that matched the entire regular expression.
* **`${BASH_REMATCH[n]}`**: Contains the substring that matched the *n*th parenthesized subexpression in the regex.

```bash theme={"dark"}
string="data-2048-xyz"
pattern='^([a-z]+)-([0-9]+)'

[[ $string =~ $pattern ]]


# Array state post-evaluation:

# ${BASH_REMATCH[0]} == "data-2048"

# ${BASH_REMATCH[1]} == "data"

# ${BASH_REMATCH[2]} == "2048"
```

The `BASH_REMATCH` array is overwritten by the shell every time the `=~` operator is executed. If the match fails, the array is cleared.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
