direnv, which is a tool for this purpose which is a statically linked executable that hooks into your shell (csh,bash, and the like)
sudo apt-get install direnv && echo "eval "$(direnv hook bash)"" >> ~/.bashrc
Now to whichever folder you'd like the environment variables to be set, add an
.direnvrc file that must have valid bash syntax. For example of your case, you can load both pyenv's version management as well as your own variables by setting your
if [[ -x "$python_root/bin/python" ]]; then
layout python "$python_root/bin/python"
echo "Error: $python_root/bin/python can't be executed."
You can see other examples at their wiki
Thanks to @ChrisKuehl in the comments for the suggestion
Another alternate approach would be to override the
PROMPT_COMMAND (as suggested in the comments by @steeldriver) to point to a function that loads your environment variable up, by adding something like this to your
if [ "$PWD" == "yourdirectorypath" ]; then
Now when you enter
yourdirectorypath, it'll automatically set
CUSTOM_ENV_VAR, when you exit out of it, it'll
unset(remove) the variable, hence that variable is only available when the current directory is