Load environment variables from .env files and launch a shell or command with them set. Supports watching for changes.
git clone https://github.com/yshngg/dotenv.git
cd dotenv
go build -o dotenv main.go
sudo install -m 755 dotenv /usr/local/bin/dotenvgo install github.com/yshngg/dotenv@latestdotenv [-f <file>] [-w] [-h] [-- <command>]-f <file>: Use custom.envfile (default:.env)-w: Watch for file changes and reload automatically--: Separator to specify custom command to run (default:$SHELL)-h: Show help
# Load .env and start default shell
dotenv
# Load specific .env file
dotenv -f .env.production
# Watch .env for changes and restart shell
dotenv -w
dotenv -w -f .env.development
# Run custom command with environment variables
dotenv -- python script.py
dotenv -- npm start
dotenv -f .env.test -- go test ./...
# Watch mode with custom command
dotenv -w -- python manage.py runserver
dotenv -w -f .env.local -- ./myapp- Reads
.envfile line by line, parsingKEY=VALUEpairs (invalid lines are skipped) - Appends variables to the environment of the spawned command
- Starts your default shell (
$SHELL) or custom command specified after-- - For bash shells (command ends with "bash"), changes prompt to
(.env) #for visual feedback - Creates a process group for proper signal delivery to child processes
- In watch mode (
-w), polls file every 100ms and reloads on changes - On file change, kills the current process group and restarts the command with updated environment
- Simple
KEY=VALUEformat only (no comments, multiline, or expansion); whitespace around=is trimmed - Invalid lines are logged and skipped
- Environment file must exist; tool exits with error if file not found
- Watch mode uses 100ms polling
- Bash prompt modification only when command ends with "bash"
- Variables set for spawned command only (not current process)
- Custom commands can be specified after
--separator - Process groups ensure proper signal delivery to child processes (uses
pkill -Pfor termination) - Informational messages logged to stderr (file changes, process termination, restarts)
The test/ directory contains example programs for testing dotenv functionality:
test/counter/- Simple counter that prints incrementing numbers (useful for testing watch mode)test/server/- HTTP server that displays environment variables
Example usage with test programs:
# Run counter with environment variables
dotenv -- go run test/counter/main.go
# Run server with environment variables in watch mode
dotenv -w -- go run test/server/main.goApache License 2.0. See LICENSE for details.