Text Processingflags: g

Unix Environment Variable Reference

Match Unix shell environment variable references in both $VAR and ${VAR} forms.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\$\\{?([A-Z_][A-Z0-9_]*)\\}?", "g");
const input = "Path is $HOME/.config";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

Pattern

regexengine-agnostic
\$\{?([A-Z_][A-Z0-9_]*)\}?   (flags: g)

Raw source: \$\{?([A-Z_][A-Z0-9_]*)\}?

How it works

\$ matches the literal dollar sign. \{? optionally matches an opening brace. ([A-Z_][A-Z0-9_]*) captures the variable name: must start with a letter or underscore, followed by letters, digits, or underscores. \}? optionally matches the closing brace.

Examples

Input

Path is $HOME/.config

Matches

  • $HOME

Input

export ${DATABASE_URL}

Matches

  • ${DATABASE_URL}

Input

no variables here

No match

Common use cases

  • Shell script static analysis
  • Dockerfile and CI config variable extraction
  • Documentation generation for environment variables
  • Config template rendering engines

Related patterns