Pass a regex as one quoted argument to grep. For Bash [[ value =~ regex ]], an unquoted pattern variable supplies regex syntax, while quoting that expansion makes its characters literal.
Reproduce the behavior safely
Run this self-contained fixture in an empty temporary directory, separate from your project. Shell examples create only named fixture files there. JavaScript examples use local values and send no network requests.
text='build-42'
pattern='^build-[0-9]+$'
printf '%s\n' "$text" | grep -E -- "$pattern"
if [[ $text =~ $pattern ]]; then printf 'regex match\n'; else exit 1; fi
if [[ $text =~ "$pattern" ]]; then
printf 'unexpected literal match\n'; exit 1
else
printf 'quoted pattern is literal\n'
fiObserved local output
build-42
regex match
quoted pattern is literalWhy the result changes
These are two interfaces, not contradictory advice about all variables. grep receives ordinary command arguments, where quoting preserves the single pattern argument. The Bash conditional has its own parsing rules and does not perform the same word splitting and pathname expansion inside [[. Quoting the right-hand expansion there changes regex interpretation.
Choose and verify the correction
First identify which matcher is running: grep basic or extended mode, Bash =~, or a different tool. Store the pattern separately from sample data, then test one expected match and one nonmatch through that exact interface. Use -- before a grep pattern that could begin with a hyphen. For a literal string search, prefer grep -F rather than constructing regex escapes by trial and error.
Limits and next checks
This fixture was executed with Bash and grep on macOS; it is not POSIX sh syntax. Bash compatibility modes and older versions can affect regex quoting behavior, so retain the runtime version when diagnosing a mismatch. Never execute a pattern through eval to repair quoting. Parsing JSON or another structured format with a regex needs a separate format-aware design.
Related: Bash Bracket Test Needs Separate Arguments: Check Spaces and Quoting.
Isolated fixture executed on macOS on November 10, 2026 (KST), with GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26). The observed output verifies this example, not every application or runtime.