How to add Comments to a .env file - Complete Guide

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# How to add Comments to a .env file - Complete Guide

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.

.env
DOMAIN=example.com ADMIN_EMAIL=admin@bobbyhadz.com DB_PORT=1234 DB_USER=bobby_hadz

env file

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.

.env
# domain and email DOMAIN=example.com ADMIN_EMAIL=admin@bobbyhadz.com # DB details DB_PORT=1234 DB_USER=bobby_hadz

add comments on separate lines

Notice that the lines that start with a hash # are greyed out.

They are interpreted as comments and are ignored.

# Adding a comment at the end of a line

You can also add a comment at the end of a line.

.env
# 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

add comments to the end of lines in env file

The screenshot shows how to add inline comments to .env files.

Note that the comments have to be placed after the key-value pair.

Everything after the hash # symbol is interpreted as a comment.

Make sure to precede inline comments with a space.

.env
# ✅ 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.

# Wrap values that contain hashes in quotes

If your value contains a hash # symbol, wrap it in quotes.

.env
DB_PORT=1234 DB_USER="bobby#hadz"

wrap values that contain hashes in quotes

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.

# Multiline comments are not available

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.

.env
# 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

comment multiple lines in env file

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.

# Notes about using .env files

Things to note when working with .env files.

  • Lines starting with a hash # symbol are ignored.
.env
# This line is ignored
  • Empty lines are ignored.
.env
# Both lines are ignored
  • Each line represents a key-value pair separated by an equal sign.
.env
KEY=VALUE ANOTHER_KEY=ANOTHER_VALUE
  • If your value contains a hash # symbol, wrap it in double quotes.
.env
DB_USER="bobby#hadz"
  • Inline comments must be preceded by a space.
.env
# ✅ Correct (has space before #) DB_PORT=1234 # My DB Port # ⛔️ Incorrect (# immediately after value) DB_PORT=1234# My DB Port
  • Quotes can be escaped with a backslash.
.env
DB_USER='It\'s him'

I've also written an article on how to add comments to your package.json file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.