direnv
direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.
Key Features
- Automatic Environment Management: Loads environment variables when you enter a directory containing a
.envrcfile and unloads them when you leave. - Shell Agnostic: Supports hooks for all common shells (bash, zsh, fish, etc.).
- Language Agnostic: Works for any programming language.
- Security: Prevents loading
.envrcfiles until explicitly authorized. - Standard Library (stdlib): Provides utility functions like
PATH_addto simplify common tasks.
Installation on macOS
Standard method for macOS is via Homebrew:
- Install via Homebrew:
- Hook into your shell: Add the hook to your shell configuration file (e.g.,
~/.zshrc). - Restart your shell or source your config file.
Basic Usage
- Create a
.envrcfile: - Authorize the file:
- Verify: The variable
FOOis now available. If youcd ..out of the directory, the variable will be automatically unset.