6

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.

5
  • 1
    Hi Ladenkov and welcome to askUbuntu! What do you exactly mean with specific folder? Can you give a more detailed example of your question? – abu_bua Jul 15 '18 at 17:36
  • 2
    Fundamentally, it's processes not directories that have environments – steeldriver Jul 15 '18 at 17:50
  • 1
    @abu_bua edited – Ladenkov Vladislav Jul 15 '18 at 18:37
  • @steeldriver I understand this, but isn't there a way to tell the process to load variable only when the user works in a special directory? – Ladenkov Vladislav Jul 15 '18 at 18:37
  • 1
    @LadenkovVladislav see Thomas Ward's answer - I guess in principle you could use the bash PROMPT_COMMAND to check pwd and set / unset variables (our source environment scripts) appropriately – steeldriver Jul 15 '18 at 18:55
9

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

2
  • Seems like it works, but is there a simplier solution? For example if i use pyenv for my directory, can the var be set automaticly with pyenv? – Ladenkov Vladislav Jul 15 '18 at 21:16
  • There are a fair number of tools that generalize the approach in this answer and seem to achieve what you're looking for, e.g. aactivator or direnv. – Chris Kuehl Jul 15 '18 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.