Dotenv File Parsing Rules and YAML Equivalents
Deep dive into .env file parsing rules including quoting, comments, multiline values, and exports. Learn how each dotenv feature maps to YAML structures.
Detailed Explanation
The dotenv format is deceptively simple but has many edge cases. Understanding the precise parsing rules is essential when converting between .env and YAML, especially since different dotenv libraries have slightly different behaviors.
A comprehensive .env file:
# Application settings
APP_NAME=DevToolbox
APP_PORT=3000
APP_URL="https://www.dev-toolbox.tech"
# Quoted strings (preserves whitespace)
GREETING=" Hello, World! "
SINGLE_QUOTED='no $expansion here'
# Comments
INLINE=value # this may or may not be a comment depending on parser
HASH_VALUE="value # not a comment"
# Multiline (double quotes only)
PRIVATE_KEY="-----BEGIN RSA KEY-----
MIIBxTCCAWug...
-----END RSA KEY-----"
# Export prefix (supported by some parsers)
export EXPORTED_VAR=exported_value
# Empty and whitespace
EMPTY=
WHITESPACE=" "
YAML equivalent:
# Application settings
APP_NAME: DevToolbox
APP_PORT: 3000
APP_URL: "https://www.dev-toolbox.tech"
# Quoted strings
GREETING: " Hello, World! "
SINGLE_QUOTED: "no $expansion here"
# Comments
INLINE: value
HASH_VALUE: "value # not a comment"
# Multiline
PRIVATE_KEY: |
-----BEGIN RSA KEY-----
MIIBxTCCAWug...
-----END RSA KEY-----
# Export prefix
EXPORTED_VAR: exported_value
# Empty and whitespace
EMPTY: ""
WHITESPACE: " "
Dotenv parsing rules mapped to YAML:
| .env Feature | .env Syntax | YAML Equivalent |
|---|---|---|
| Simple value | KEY=value |
KEY: value |
| Double quoted | KEY="value" |
KEY: "value" |
| Single quoted | KEY='value' |
KEY: 'value' or KEY: "value" |
| Comment | # comment |
# comment |
| Empty value | KEY= |
KEY: "" or KEY: (null) |
| Multiline | KEY="line1\nline2" |
KEY: |\n line1\n line2 |
| Export prefix | export KEY=val |
Not applicable |
| Variable expansion | KEY=$OTHER |
Not supported natively |
Parser differences to be aware of:
- Node.js dotenv: No inline comments, no multiline, no export prefix, no variable expansion
- Python dotenv: Supports inline comments (with space before
#), multiline, export prefix, and variable expansion - Docker: Supports inline comments, no multiline, no export prefix, basic variable expansion
- Ruby dotenv: Full feature support including command substitution
When converting from .env to YAML, always test with the same parser your application uses to ensure behavior matches.
Use Case
Creating a comprehensive YAML configuration schema from an existing .env.example file, documenting all available configuration options with their types, defaults, and descriptions for team onboarding.