Shell extension that automatically loads and unloads environment variables based on current directory
direnv is a shell extension that automatically manages environment variables based on your current directory. It monitors for .envrc files in the current and parent directories, loading their exported variables when you enter a directory and unloading them when you leave.
The tool works by checking for .envrc files before each shell prompt and executing them in a bash sub-shell to capture environment changes. It supports all major shells including bash, zsh, fish, tcsh, elvish, powershell, murex, and nushell. direnv requires explicit authorization of .envrc files through direnv allow as a security measure to prevent automatic execution of untrusted code.
direnv includes a standard library of utility functions accessible within .envrc files, such as PATH_add for path manipulation and dotenv for loading .env files. Users can extend functionality by creating custom functions in ~/.config/direnv/direnvrc. The tool is compiled as a single static executable for performance and supports creating isolated development environments, loading 12factor app configurations, and managing deployment secrets on a per-project basis.