LinuxCommandLibrary

direnv

per-directory environment variable manager

TLDR

Enable direnv in your shell (add to shell config)

$ eval "$(direnv hook bash)"
copy
Create a new .envrc file
$ direnv edit .
copy
Allow the current directory's .envrc
$ direnv allow
copy
Deny/revoke an .envrc
$ direnv deny
copy
Reload the current environment
$ direnv reload
copy
Export current environment as shell commands
$ direnv export bash
copy
Show environment diff when changing directories
$ direnv status
copy
Print the current .envrc being used
$ direnv show
copy

SYNOPSIS

direnv [command] [arguments]

DESCRIPTION

direnv is an environment switcher that loads and unloads environment variables based on the current directory. When you enter a directory containing an .envrc file, direnv automatically exports its variables into your shell. When you leave, it unloads them.
The .envrc file is a bash script that typically sets environment variables using export. Direnv provides a standard library of functions for common tasks like setting PATH, loading .env files, using nix-shell, or activating Python virtual environments.
For security, .envrc files must be explicitly allowed before they're executed. This prevents malicious code from running when entering untrusted directories. The allow command whitelists a file based on its content hash; any change requires re-approval.
Integration requires adding a shell hook to your configuration (e.g., ~/.bashrc). The hook modifies the prompt to show when direnv is active and handles automatic loading/unloading. Direnv supports bash, zsh, fish, tcsh, elvish, and other shells.
Common use cases include project-specific environment variables, automatic virtual environment activation, and development environment configuration.

PARAMETERS

allow [path]

Approve an .envrc file to be loaded. Security measure to prevent executing arbitrary code.
deny [path]
Revoke approval for an .envrc file.
edit [path]
Open .envrc in $EDITOR and allow it after editing.
reload
Manually reload the current directory's environment.
status
Show direnv's current status and active .envrc.
export shell
Export the current environment for the specified shell (bash, zsh, fish, etc.).
hook shell
Print the shell hook code to enable direnv.
show
Display the currently loaded .envrc file.
version
Print direnv version.
prune
Remove old allowed file entries.
fetchurl url [integrity]
Fetch a URL with caching and optional integrity check.
stdlib
Print the stdlib functions available in .envrc.

CONFIGURATION

.envrc

Per-directory environment configuration script loaded when entering the directory.
~/.config/direnv/direnv.toml
Global direnv configuration for whitelist and settings.
~/.config/direnv/direnvrc
Global shared functions and settings available to all .envrc files.

CAVEATS

Shell hook must be added to config for automatic operation. .envrc files must be explicitly allowed (security feature). Changes to .envrc require re-allowing. Some shell features may not work identically in .envrc. Large environments may cause slight delay when changing directories.

HISTORY

direnv was created by Jonas Pfenniger (zimbatm) around 2013 to solve the problem of managing per-project environment configurations. It was inspired by the need to switch between different development environments without manually sourcing files. The tool has become popular in the DevOps and development communities, especially with nix integration.

SEE ALSO

source(1), env(1), export(1), nix-shell(1), virtualenv(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community