Skip to main content
AllDevToolsHub
Back to all patterns

Environment Variable Name Validation

Validation

Validates POSIX-compliant environment variable names: uppercase letters, digits, and underscores, not starting with a digit.

/^[A-Z_][A-Z0-9_]*$/

How it works

POSIX environment variable names consist of uppercase letters, digits, and underscores, and must not start with a digit. By convention, environment variables are uppercase. This pattern enforces the strict POSIX convention.

Test Cases

Should Match

  • DATABASE_URL
  • PORT
  • NODE_ENV
  • AWS_ACCESS_KEY_ID

Should NOT Match

  • lowercase_var
  • 1INVALID
  • has-hyphen
  • has space

Quick Summary

Validates POSIX environment variable names: uppercase letters, digits, and underscores only, must start with a letter or underscore. Rejects lowercase, hyphens, and names starting with digits.

Key Takeaways

Key Takeaways

  • Must start with an uppercase letter or underscore (not a digit)
  • Only uppercase letters (A-Z), digits (0-9), and underscores (_) allowed
  • Lowercase env vars are technically valid in some shells but not POSIX-compliant
  • Variables starting with _ are reserved by convention for system use
Use Cases

When to use it

  • Validating .env file keys in dotenv parsers
  • Checking environment variable names in CI/CD configuration UIs
  • Linting infrastructure-as-code templates
Watch out

Common Mistakes

  • Using hyphens in env var names, they are not valid in POSIX and break shell expansion
  • Not validating env var names before passing to child processes
FAQ

Environment Variable Name Validation, Frequently Asked

Can environment variable names be lowercase?

Technically yes in most shells, but POSIX convention is uppercase. Lowercase names risk conflicts with shell variables.

What is the maximum length of an environment variable name?

POSIX does not specify a maximum, but Linux limits the total environment size to ~128KB. Keep names concise.