Ubuntu – Set permanent environment variable for a folder

environment-variables

There are a lot of duplicate questions about setting permanent environment variables, but no questions about setting them for a specific folder.

So, how to set an environment variable for a specific folder?

Clearifying: I want my CUSTOM_ENV_VAR to activate only when i work in a specific dir .../custom_dir/. So when i launch programs in the folder, programs use this CUSTOM_ENV_VAR, when i launch outside – programs do not use it.

Best Answer

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

Related Question