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
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).
Python (re) code
import re
pattern = re.compile(r"\$\{?([A-Z_][A-Z0-9_]*)\}?")
input_text = "Path is $HOME/.config"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\$\{?([A-Z_][A-Z0-9_]*)\}?`)
input := `Path is $HOME/.config`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\$\{?([A-Z_][A-Z0-9_]*)\}? (flags: g)Raw source: \$\{?([A-Z_][A-Z0-9_]*)\}?
How it works
Examples
Input
Path is $HOME/.configMatches
$HOME
Input
export ${DATABASE_URL}Matches
${DATABASE_URL}
Input
no variables hereNo 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
HCL / Terraform Variable Reference
Text ProcessingMatch Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.
JavaScript Variable Declaration
Text ProcessingMatch JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
Markdown Link
Text ProcessingMatch Markdown links [link text](url) and capture both the display text and the URL.
Single-Line Comment (// or #)
Text ProcessingMatch single-line comments using either the `//` (C-family) or `#` (shell, Python, Ruby, YAML) marker.