opyeb envmask

.env syntax and the mistakes that break it

2026-11-29 · 2 min read

One line: a .env file is one KEY=VALUE pair per line, and any value containing spaces, # or a newline needs quotes.

The basic rules

A dotenv file holds one key per line with a single equals sign. Keys use letters, digits, underscores and dots; spaces around the equals sign are ignored. Leading and trailing spaces in the value are trimmed, so keep them only inside quotes.

A line starting with # is a comment. Inside an unquoted value, a space followed by # starts a trailing comment.

# comment
APP_ENV=production
PORT=8080
API_BASE=https://api.example.com
GREETING="hello world"

Quoting rules

Double quotes interpret escapes: \n becomes a newline, \t a tab, \" a literal quote. Single quotes keep the text exactly, so \n stays two characters.

Quote any value containing #, spaces, $ or a quote. Otherwise the # truncates the value or the shell expands $ as a variable.

How quoting changes the value
WrittenValue
A=plainplain
A="a b"a b
A="line1\nline2"two lines
A='line1\nline2'one line keeping the backslash
A=abc#defabc#def (# without a space is part of the value)
A=abc #defabc (rest is a comment)

Multi-line values

Certificates and private keys span lines. Inside double quotes you can put real newlines or join lines with \n. Parser support varies, so check that the parser in your deployment environment reads multi-line double-quoted values.

For a multi-line PEM it is safer to store a path instead: TLS_KEY_PATH=/run/secrets/tls.key

export and framework prefixes

A file that is sourced by a shell may use export KEY=VALUE. Node's dotenv, Next.js, Vite and Docker Compose differ slightly in whether they accept the prefix.

Next.js and Vite expose only prefixed variables to the browser bundle (NEXT_PUBLIC_, VITE_). Never place a secret behind those prefixes.

Parser differences
ToolBehaviour
Node dotenvNo export prefix; multi-line double quotes supported
Docker Composeenv_file comments and quotes, ${VAR} interpolation
Next.jsOnly NEXT_PUBLIC_ reaches the browser
ViteOnly VITE_ is exposed, per-mode .env.production
KubernetesUse stringData on a Secret instead of a .env file

After you edit

Check that the values survived the edit. Comparing the key structure with a masking tool catches typos that renamed a key, without exposing the values.

Never commit real values. Keep a keys-only .env.example in the repository.

Questions

How do I put a # in a value?

Without a preceding space many parsers keep it, but behaviour varies. Double quotes are the reliable answer.

What is the difference between .env and .env.local?

.env holds defaults; .env.local is a personal override and belongs in .gitignore.

Sources

Every example here can be pasted into the workbench as it is.

Open the workbench