exportenv is a simple, easy-to-use utility for reading environment variables from .env files and exporting them to the current shell session. It supports multiple .env files, variable expansion, overriding variables, and evaluating environment variables without exporting them.
Run the tool to read environment variables from .env files and export them into your session. By default, exportenv uses .env in the current directory if no file is specified.
--env-file, -f <path>: Specify one or more paths to.envfiles, processed in order. If no files are provided,exportenvdefaults to using.envin the current directory.--override, -o: Allow variables in succeeding.envfiles to overwrite variables from earlier ones.--no-expand: Disable variable expansion for${VAR}syntax in.envvalues.-v <KEY=VALUE>: Set variables directly from the command line, which take precedence over.envfiles.--: Use--before a command to execute it with the loaded environment variables.
Load variables from .env in the current directory and export them:
eval $(./exportenv)
Load variables from specified .env files:
eval $(./exportenv --env-file /path/to/.env1 --env-file /path/to/.env2)
Use multiple .env files where later files override variables from earlier ones:
eval $(./exportenv --env-file .env --env-file .env.local --override)
Define variables directly via -v, which take precedence over .env files:
eval $(./exportenv -v NAME=JohnDoe -v ENV=production)
Run a command with the variables loaded from .env. Use -- before the command to pass it to exportenv:
./exportenv --env-file .env -- my_command --option=value
A valid .env file should follow these guidelines:
- Each line should be in the
KEY=VALUEformat. - Comments start with
#and are ignored. - Empty lines are skipped.
- Values can be:
- Unquoted:
FOO=bar baz - Double-quoted:
FOO="bar baz", supporting escape sequences like\n,\t, and\". - Single-quoted:
FOO='bar baz', which takes the value literally, including special characters.
- Unquoted:
Example .env file:
# Application settings
APP_NAME="My Application"
APP_ENV=production
# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_USER="my_user"
DB_PASS='my$ecret'
# Multiline value for a private key
PRIVATE_KEY="```--BEGIN PRIVATE KEY```--\nMIIEvQIBADANB ... \n```--END PRIVATE KEY```--"
- Run a command with variables from multiple
.envfiles (e.g.,.env,.env.local) and command-line overrides:
./exportenv --env-file .env --env-file .env.local -v MODE=debug -- my_command --option=value
- Load and export variables from
.envfiles with overrides and run a server:
eval $(./exportenv --env-file .env --env-file .env.production --override) && npm start
.envfiles are processed in the order they’re specified, unless--overrideis set.- Variable expansion (
${VAR}syntax) is enabled by default but can be disabled with--no-expand. - Always use
evalwhen loading variables to ensure they are exported into the current session.