direnv

From RaySoft

direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory. [1]

Documentation

Syntax

direnv COMMAND [ARGUMENTS ...]

Sub-commands

allow [PATH_TO_RC], grant [PATH_TO_RC], permit [PATH_TO_RC]
Grants direnv permission to load the given .envrc or .env file.
block [PATH_TO_RC], deny [PATH_TO_RC], disallow [PATH_TO_RC], revoke [PATH_TO_RC]
Revokes the authorization of a given .envrc or .env file.
hook SHELL
Used to setup the shell hook.
reload
Triggers an env reload.

Examples

Change the default editor in the ~/tmp directory
  1. Create an .envrc file
    vi "${HOME}/tmp/.envrc"
    
    #!/bin/bash
    
    export EDITOR='nano'
    
  2. Change the directory
    cd "${HOME}/tmp"
    
    Output
    direnv: error /Users/leuenbe/tmp/.envrc is blocked. Run `direnv allow` to approve its content
    
  3. Allow the .envrc file to be executed
    direnv allow
    
    Output
    direnv: loading ~/tmp/.envrc
    direnv: export ~EDITOR
    

References

  1. Project contributors. "direnv - unclutter your .profile." direnv project. https://direnv.net/ (accessed 24.01.2024)