Using direnv
Install 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 .direnvrc
to:
use_python() {
local python_root=$PYENV_ROOT/versions/$1
load_prefix "$python_root"
if [[ -x "$python_root/bin/python" ]]; then
layout python "$python_root/bin/python"
else
echo "Error: $python_root/bin/python can't be executed."
exit
fi
}
export CUSTOM_VAR="xyz";
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 .bashrc
prmfn() {
if [ "$PWD" == "yourdirectorypath" ]; then
export CUSTOM_ENV_VAR=value
else
unset CUSTOM_ENV_VAR
fi
}
export PROMPT_COMMAND=prmfn
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 yourdirectorypath
Best Answer
The
/etc/environment
update will only work on the next session, it's not automatically reloaded.Which means you cannot change it for sessions that have already started for other users.
If you want to "reload" whatever is in the
/etc/environment
you need the following command:source /etc/environment
But again it will only work for your own current session, other users won't be affected until they start a new session or run the above command in their own session.