Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active May 12, 2024 01:30
Show Gist options
  • 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

@cschar
Copy link

cschar commented May 12, 2024

3rd result on google, thanks ☕

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