📜  direnv - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:25.081000             🧑  作者: Mango

direnv - Shell-Bash

Direnv is an extension for Bash or Zsh that allows you to easily manage environment variables.

Why use direnv?
  • Direnv makes it easy to manage your environment variables for different projects or workspaces.
  • It allows you to set and unset environment variables automatically when you enter and exit a directory.
  • It can help you avoid mistakes caused by forgetting to set or unset environment variables.
Installation

Direnv can be installed using any of the following methods:

  • Using a package manager (e.g. apt, Homebrew, etc.)
  • Downloading the binary from the direnv releases page
  • Building from source
Usage
  1. Create a .envrc file in your project directory.
  2. Add your desired environment variables to the .envrc file.
  3. Run direnv allow to allow the .envrc file to be loaded.
  4. Whenever you enter the directory, direnv will automatically load the .envrc file and set the environment variables. Whenever you exit the directory, direnv will unset the environment variables.

For example, the following .envrc file could be used to set the NODE_ENV variable to development:

export NODE_ENV=development

When you enter the directory containing this .envrc file, direnv will automatically set the NODE_ENV variable to development.

Security

Be careful when using direnv with untrusted .envrc files. By default, direnv will not load any .envrc file that is not explicitly allowed by the user.

Conclusion

Direnv is a powerful tool for managing environment variables in Bash and Zsh. With direnv, you can easily set and unset environment variables for different projects or workspaces, thereby avoiding mistakes caused by forgetting to set or unset environment variables.