Last updated: Apr 5, 2024
Reading time·3 min

Use the hash # symbol to add comments to a .env file.
Anything written after the hash # symbol is interpreted as a comment in
.env files.
For example, assuming you want to add comments to the following .env file.
DOMAIN=example.com ADMIN_EMAIL=admin@bobbyhadz.com DB_PORT=1234 DB_USER=bobby_hadz

The syntax in a .env file is formatted as key=value.
You can add comments on separate lines by starting the line with a hash #
symbol.
# domain and email DOMAIN=example.com ADMIN_EMAIL=admin@bobbyhadz.com # DB details DB_PORT=1234 DB_USER=bobby_hadz

Notice that the lines that start with a hash # are greyed out.
They are interpreted as comments and are ignored.
You can also add a comment at the end of a line.
# domain and email DOMAIN=example.com # My domain ADMIN_EMAIL=admin@bobbyhadz.com # DB details DB_PORT=1234 # My DB Port DB_USER=bobby_hadz

The screenshot shows how to add inline comments to .env files.
Everything after the hash # symbol is interpreted as a comment.
Make sure to precede inline comments with a space.
# ✅ correct (has space before #) DB_PORT=1234 # My DB Port # ⛔️ incorrect (# immediately after value) DB_PORT=1234# My DB Port
Notice that there is no space between the value and the hash # symbol in the
second statement.
This often causes syntactical errors and difficult-to-debug issues.
If your value contains a hash # symbol, wrap it in quotes.
DB_PORT=1234 DB_USER="bobby#hadz"

Notice that the part after the hash # symbol is not greyed out.
This is because when wrapping the value in quotes, the hash # is interpreted
as the literal character and not as a start of a comment.
This syntax can be used to add comments to .env files that are interpreted by
any language or framework, e.g. Node.js, Python, Laravel, Docker etc.
I've also written articles on:
The same applies when using Docker.
Lines that start with a hash # symbol are interpreted as comments and are
ignored in Docker environment files.
It should be noted that there isn't a character sequence that allows you to
comment multiple lines in a .env file.
If you need to comment out multiple lines, you have to start each line with a
hash # symbol.
# domain and email DOMAIN=example.com # My domain ADMIN_EMAIL=admin@bobbyhadz.com # DB details DB_PORT=1234 # My DB Port DB_USER=bobby_hadz # first line # second line # third line

The screenshot shows how we have to start each line with a hash symbol as there
is no special syntax for multiline comments in .env files.
.env filesThings to note when working with .env files.
# symbol are ignored.# This line is ignored
# Both lines are ignored
KEY=VALUE ANOTHER_KEY=ANOTHER_VALUE
# symbol, wrap it in double quotes.DB_USER="bobby#hadz"
# ✅ Correct (has space before #) DB_PORT=1234 # My DB Port # ⛔️ Incorrect (# immediately after value) DB_PORT=1234# My DB Port
DB_USER='It\'s him'
I've also written an article on
how to add comments to your package.json file.
You can learn more about the related topics by checking out the following tutorials: