Sub topic 10
.env Syntax Details: Comments, Quotes, and Multiple Lines
This chapter assumes you know how Compose reads a .env file for interpolation and how ${VAR} expressions work. Here you will learn the line-level rules of the file: how comments end, what each kind of quote does, and how to store a value that spans several lines.
Basic line format
Each line in a .env file is NAME=value. Compose ignores blank lines and lines that start with #. Whitespace around the line is trimmed. An empty value (NAME=) sets the variable to an empty string.
# Database settings
POSTGRES_USER=app
POSTGRES_PASSWORD=change-me
EMPTY_VALUE=Comments
A comment can follow a value on the same line, with one condition for unquoted values. The # must be preceded by a space. Without that space, Compose treats the # as part of the value.
DB_PORT=5432 # this part is a comment
COLOR=abc#defHere DB_PORT is 5432, and COLOR is abc#def. This matters for passwords. A password such as pa #ss loses everything from the # onward unless you quote it.
For quoted values, the comment goes after the closing quote:
API_URL="https://example.com/a#frag" # comment after the quoteQuotes
Quotes control three things: whether spaces and # are kept, whether escape sequences are processed, and whether $ is interpolated.
| Form | Example | Behavior |
|---|---|---|
| Unquoted | `NAME=value` | Trailing spaces trimmed. ` #` starts a comment. `${VAR}` is interpolated. |
| Double quotes | `NAME="a b # c"` | Spaces and `#` kept. Escapes such as `\n`, `\t`, and `\\` are processed. `${VAR}` is interpolated. |
| Single quotes | `NAME='$HOME #x'` | Taken literally. No escapes, no interpolation. |
Use single quotes when a value contains a $ that must reach the container unchanged:
GREETING="Hello,\nworld"
LITERAL='$HOME stays as written'
BASE_URL=http://localhost
FULL_URL="${BASE_URL}/api"GREETING contains a real newline between Hello, and world. LITERAL keeps the text $HOME exactly. FULL_URL becomes http://localhost/api, because double quotes allow interpolation of a variable defined earlier in the file.
Multiple lines
A double-quoted value can run across several lines. Compose keeps the line breaks inside the quotes. This is useful for PEM keys and certificates.
TLS_KEY="-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASC
-----END PRIVATE KEY-----"If you only need a short value with line breaks, the \n escape inside double quotes is simpler.
Verifying the result
Do not guess how a line is parsed. Reference the variables in a Compose file and print the resolved result.
services:
app:
image: my-app:latest
environment:
DB_PORT: ${DB_PORT}
COLOR: ${COLOR}
FULL_URL: ${FULL_URL}docker compose configWith the .env lines from this chapter, the output contains DB_PORT: "5432", COLOR: abc#def, and FULL_URL: http://localhost/api. If a value is cut short or contains stray quotes, the .env line is the cause.
Common mistakes
- An unquoted value with
#loses the text after the space. Quote it. - A
$in a password gets interpolated. Use single quotes, or write$$in a double-quoted or unquoted value. - A missing closing quote on a multi-line value swallows the lines below it into one variable.
Exercise
Your .env contains PASSWORD=ab #cd$ef. The container receives only ab. Write one line that stores the full value ab #cd$ef without Compose changing it.
Show answer
Use single quotes: PASSWORD='ab #cd$ef'. Single quotes keep the space and #, and they stop $ef from being interpolated.