← 返回命令列表

Linux command

direnv 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Enable direnv

eval "$(direnv hook bash)"

Create a new .envrc file

direnv edit .

Allow the current directory's .envrc

direnv allow

Deny/revoke an .envrc

direnv deny

Reload the current environment

direnv reload

Export current environment

direnv export bash

Show environment diff

direnv status

Print the current .envrc

direnv show

说明

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.

参数

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.

FAQ

What is the direnv command used for?

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.

How do I run a basic direnv example?

Run `eval "$(direnv hook bash)"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does allow _path_ do in direnv?

Approve an .envrc file to be loaded. Security measure to prevent executing arbitrary code.