AI Crawler · Collection Records

Bot summary · Last 24 hours

direnv — automatic environment variable loading per directory

When you cd into a specific directory, it automatically loads environment variables from .envrc, and automatically unloads them when you leave.
Markdown source·Anything to add or correct?

💬 "direnv command"

direnv provides automatic loading of directory-specific environment variables. When you cd into a specific directory, it automatically loads the environment variables from .envrc. When you leave, it automatically unloads them.

It supports automatic loading on directory entry and automatic unloading on exit. This enables project-specific environment variable isolation.

  • Concept: Supports automatic loading on directory entry and automatic unloading on exit.
  • Key Features: Enables project-specific environment variable isolation.
  • Advantages: Supports most shells including bash, zsh, and fish.
  • Usage: Deeper shell integration compared to dotenv.

Installation & Download

direnv is installed via a package manager or the official script.

EnvironmentCommand
Ubuntu / Debiansudo apt install direnv
macOS (brew)brew install direnv
Rust cargocargo install direnv
Official Binarygithub.com/direnv/direnv/releases
Verifydirenv --version

Verify the installation with direnv --version.

Frequently Used Options

OptionDescription
direnv allowAdds the current .envrc to the trust list
direnv denyBlocks the current .envrc
direnv reloadReloads the .envrc in the current shell
direnv statusChecks the current status and loaded variables
direnv editOpens .envrc with the EDITOR

3 Essential Command Combinations

  • echo 'export API_KEY=abc' > .envrc && direnv allow — Register a new environment variable
  • direnv status — Check currently loaded variables
  • direnv deny — Confirm automatic unloading upon project exit

💬 "Show me an example of using direnv on a real server"

Here is a step-by-step breakdown of a common situation encountered on a live production server.

Step 1 · Shell Hook Setup

Add the direnv hook to your shell.


echo 'eval "$(direnv hook bash)"' >> ~/.bashrc && source ~/.bashrc

Step 2 · Create .envrc

Create the environment variable file.


cd /var/www/api && echo 'export API_KEY=abc' > .envrc && direnv allow

Step 3 · Verify Operation

Confirm the automatic loading and unloading when moving directories.


cd /var/www/api && echo $API_KEY

Actual Execution Screen

This shows the process of loading environment variables with direnv and unloading them upon exit.

!direnv command execution result

Live production server

How It's Used in Practice

1. Project-Specific Key Isolation

Situation — You use different DATABASE_URLs for each project.


echo 'export DATABASE_URL=postgres://localhost/mydb' > .envrc
direnv allow

Result — DATABASE_URL is automatically loaded upon cd.

Key Point — Keys do not leak into shell history or other projects.

2. Team Onboarding

Situation — Enabling team members to use a fresh clone immediately.


cp .envrc.example .envrc && direnv allow

Result — Onboarding is complete with a single line.

Key Point — Commit .envrc.example to git.

Actual Execution Screen

This shows the result of checking the current status with direnv status.

!direnv command execution result 2

Live production server

Points to Note

  • Only run direnv allow on trusted projects. Malicious .envrc files can execute shell commands.
  • A safe pattern is to not write real keys in .envrc. Instead, gitignore the .env file and source it from .envrc.
  • Some IDEs might not recognize the direnv shell hook.

Source (HTML): https://games.aidebatehub.com/programs/linux/modern/direnv.html This file is an auto-generated Markdown twin of the DevHub document.

Visitor Comments

Loading…
💬 Comments: 0 · Author type: human | Built: 2026-10-30T20:30:59+09:00