Need help with dotenv?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

291 Stars 20 Forks MIT License 234 Commits 33 Opened issues


Library to help supply environment variables in testing and development

Services available


Need anything else?

Contributors list


CI codecov

Achtung! This is a v0.* version! Expect bugs and issues all around. Submitting pull requests and issues is highly encouraged!

Quoting bkeepers/dotenv:

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

This library is meant to be used on development or testing environments in which setting environment variables is not practical. It loads environment variables from a

file, if available, and mashes those with the actual environment variables provided by the operative system.


The easiest and most common usage consists on calling

when the application starts, which will load environment variables from a file named
in the current directory or any of its parents; after that, you can just call the environment-related method you need as provided by

If you need finer control about the name of the file or its location, you can use the

methods provided by the crate.

provides the
macro, which behaves identically to
, but first tries to load a
file at compile time.



file looks like this:
# a comment, will be ignored

You can optionally prefix each line with the word

, which will conveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

extern crate dotenv;

use dotenv::dotenv; use std::env;

fn main() { dotenv().ok();

for (key, value) in env::vars() {
    println!("{}: {}", key, value);


Variable substitution

It's possible to reuse variables in the

file using
syntax. The syntax and rules are similar to bash ones, here's the example:

Non-existing values are replaced with an empty string

RESULT=$NOPE #value: '' (empty string)

All the letters after $ symbol are treated as the variable name to replace

RESULT=$VAR #value: 'one'

Double quotes do not affect the substitution

RESULT="$VAR" #value: 'one'

Different syntax, same result

RESULT=${VAR} #value: 'one'

Curly braces are useful in cases when we need to use a variable with non-alphanumeric name

RESULT=$VAR_2 #value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbol RESULT=${VAR_2} #value: 'two'

The replacement can be escaped with either single quotes or a backslash:

RESULT='$VAR' #value: '$VAR' RESULT=$VAR #value: '$VAR'

Environment variables are used in the substutution and always override the local variables

RESULT=$PATH #value: the contents of the $PATH environment variable PATH="My local variable value" RESULT=$PATH #value: the contents of the $PATH environment variable, even though the local variable is defined

Dotenv will parse the file, substituting the variables the way it's described in the comments.

Using the


to your dependencies, and add the following to the top of your crate:
extern crate dotenv_codegen;

Then, in your crate:

fn main() {
  println!("{}", dotenv!("MEANING_OF_LIFE"));

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.