Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active September 16, 2023 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/c46ee7f067cd8a856f9f71121b9621bf to your computer and use it in GitHub Desktop.
Save kenmori/c46ee7f067cd8a856f9f71121b9621bf to your computer and use it in GitHub Desktop.
how to use direnv

how to use direnv

The direnv is package which only use to set environment values in any directory.

When you use 'cd' command for running in some project.

It will set environment values.

For example,

cd Desktop/moritakenji/
morita-no-MacBook-Pro :: Desktop/moritakenji/ » echo ${NAME} <-run
(print is empty) <-console
cd Desktop/moritakenji/sandbox
direnv: loading .envrc <-console
direnv: export +NAME <-console
morita-no-MacBook-Pro :: Desktop/moritakenji/sandbox » echo ${NAME} <-run
morita <- console

1.install

brew install direnv

2.write command in .bashrc or .zhrc

in this case zhrc

# reading direnv
eval "$(direnv hook zsh)"

don't forget to update it with source

if you using bash now, replace zsh to bash

3.put .envrc at any place you want it

set environment values as NAME, the value is morita

Notice that it is not srounded some single qute.

export NAME=morita

4.move to current directory with cd

direnv: loading .envrc
direnv: export +NAME

you can see setting it and then, try to print NAME

5.run

echo ${NAME}
morita

if an error occures like some fllowing.

direnv: error .envrc is blocked. Run `direnv allow` to approve its content.

run $ direnv allow

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment