Understand .env file structure and interpolation
masterThe .env file supports standard assignments, comments, and advanced interpolation.
Assignments and Comments
KEY=value- Comments can be at the end of a line:
KEY=value # comment - A
#immediately following=is treated as a comment:KEY=#comment - Unquoted values can include
#if it is not the first character after the value:KEY=value#notcomment #actualcomment
Interpolation Syntax
Interpolation supports default, required, and alternative values:
- Default value:
${ENVVAR:-default}or${ENVVAR-default} - Required (exit with error if empty):
${ENVVAR:?error}or${ENVVAR?error} - Alternative (use if set):
${ENVVAR:+alternative}
Interpolation can be nested, e.g., ${VARIABLE:-${FOO:-default}}.
Quoting Rules
- Double quotes (
""): Supports interpolation, whitespace, escaped characters, and byte codes. - Single quotes (
''): Supports whitespace, but no interpolation, escaped characters, or byte codes. Use this for truly raw values. - Unquoted: Supports interpolation and inline whitespace, but no quote characters, escaped characters, or byte codes.
Byte Codes (Double Quotes Only)
You can declare Unicode characters using hex codes:
- UTF8:
"\xF0\x9F\x9A\x80"(🚀) - UTF16:
"\uae"(®) - UTF32:
"\U1F680"(🚀)