Direnv not loading environment when shell starts in a directory with .envrc
Asked Answered
R

3

17

Imagine I am in a shell with the working directory set to a directory that contains an .envrc file. When I now open up a new tab in my terminal emulator, a new shell is launched into the same working directory as the original shell.

For some reason, the .envrc file is not loaded in this new shell. If manually exit the working directory and enter it again, the environment variables are then loaded.

Is there a way to make the variables load automatically when the shell is created?

I am running MacOS Mojave (10.14.4) with direnv 2.20.1, using bash as my shell. I am loading the direnv hooks through my .bashrc.

Remillard answered 3/7, 2019 at 14:59 Comment(1)
What is the output of direnv status in the original shell vs the new shell?Minstrel
X
13

Under macOS Mojave (and probably older versions), saving a .bashrc has no effect. Instead, use a .bash_profile.

Xeric answered 10/7, 2019 at 14:44 Comment(1)
saving the hook in the .bashrc worked fine in CatalinaSelene
J
17

I found in my zsh environment I needed to put export in front of each variable in .envrc

VAR=x

doesn't work

Instead, use:

export VAR=x
Jabalpur answered 22/2, 2022 at 8:1 Comment(2)
Your solution works, thanks, but it shouldn't work this way, standing to the documentation...Zorn
When using a .envrc file, you need to use the export command for each env var. However, you can load a .env file instead, and using this type of file, you do not need to include the export command. Loading .env files requires specifying a load_dotenv=true in the direnv TOML config.Hyaena
X
13

Under macOS Mojave (and probably older versions), saving a .bashrc has no effect. Instead, use a .bash_profile.

Xeric answered 10/7, 2019 at 14:44 Comment(1)
saving the hook in the .bashrc worked fine in CatalinaSelene
G
8

Is there a way to make the variables load automatically when the shell is created

you can add this to your .bashrc

eval "$(direnv hook bash)"

it activate a hook on at the start of your bash. If your bashrc is not loaded look at the bash_profile / bashrc to see which calls whom.

you can try to " source ~/.bashrc" to see if its your eval command that is not well written or your bashrc not called.

Goff answered 13/12, 2021 at 9:32 Comment(2)
Thanks for suggestion. This also works with zsh eval "$(direnv hook zsh)"Obituary
Thanks, you can try to " source ~/.bashrc" is what helped meEctoblast

© 2022 - 2024 — McMap. All rights reserved.