6

This is a .bashrc question. I would like to set "export FOO=bar" for .bashrc in a particular directory like .rvmrc.

I tried below.

$ touch ~/foo/.bashrc
$ echo 'export RAILS_ENV=development' >> ~/foo/.bashrc
$ cd foo/
$ env|grep RAILS_ENV

But RAILS_ENV shall be set nothing in this case.

If I set onto .rvmrc instead of .bashrc, it pass! But it is better way to set onto .bashrc because I do not need to install rvm environment.

Any solutions?

diveintohacking
  • 4,783
  • 6
  • 29
  • 43

4 Answers4

15

in your bashrc set this:

PROMPT_COMMAND='[[ $PWD == "/foo/bar/" ]] && export FOO=BAR || unset FOO'

The contents of the PROMPT_COMMAND variable will be executed every time your prompt is rewritten ( just before it's written actually ) the command above checks the $PWD variable ( which holds the current working directory of your shell ) against "/foo/bar" if it matches it exports your variable if it doesn't then the variable is unset.

EG

peteches@yog-sothoth$ PROMPT_COMMAND='[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO'
peteches@yog-sothoth$ pwd
/home/peteches
peteches@yog-sothoth$ cd test
peteches@yog-sothoth$ pwd
/home/peteches/test
peteches@yog-sothoth$ env | grep FOO
6:FOO=BAR
73:PROMPT_COMMAND=[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO
peteches@yog-sothoth$ cd ../
peteches@yog-sothoth$ pwd
/home/peteches
peteches@yog-sothoth$ env | grep FOO
72:PROMPT_COMMAND=[[ $PWD == "/home/peteches/test" ]] && export FOO=BAR || unset FOO
peteches@yog-sothoth$ 
peteches
  • 3,447
  • 1
  • 13
  • 15
  • Cool! This is what I want! – diveintohacking Jan 22 '13 at 16:48
  • @gipcompany, if this is the correct answer to your question, reward the answer by accepting it: click the checkmark beside the answer. – glenn jackman Jan 22 '13 at 22:16
  • I want to combine this answer with the mycd() answer below in order to set up multiple directories with different env vars (and get away from the one-liner syntax). Is it possible to assign a function like Davide's mycd() to PROMPT_COMMAND? – notbrain Jun 26 '15 at 21:49
  • This is a reasonable approach, but it might be convenient to make it more general. Since the OP seems to want $dir/.bashrc to apply, perhaps change PROMPT_COMMAND to look for `./.bash-dir-cfg` (or similar) and source it if it exists. – William Pursell Jul 15 '20 at 16:24
7

If you don't mind to use a workaround, add this to your .bash_profile

mycd()
{
    cd $1
    if [ "$(pwd)" == "/your/folder/that/needs/env" ]; then
        export RAILS_ENV=development
    else
        export RAILS_ENV=
    fi;
}
alias cd=mycd

Everytime you move to a certain folder this will set your env variable or whatever you want

Davide Berra
  • 6,387
  • 2
  • 29
  • 50
  • Nice! I also like that environment variables are only set once, when you `cd` into the directory, instead of before any command as seems to be the case with the accepted answer. – Kurt Peek Nov 06 '18 at 18:42
4

First, AFAIK, bash won't search for a .bashrc file in any other directory but your home -- at least, not by default.

Secondly, after writing new entries to your .bashrc, you should source .bashrc the file, so that modifications take place.

Rubens
  • 14,478
  • 11
  • 63
  • 92
  • 1
    Thanks for your explanation, .bashrc can not be loaded automatically in case of 'cd foo'. – diveintohacking Jan 22 '13 at 16:53
  • @gipcompany You're welcome, and that's the exact reason why you had no output after greping for `RAILS_ENV`. As some answer presented, you may add the `source'ing` step after each `cd`, but in your particular case it may be better to simply source the `.bashrc` content manually. – Rubens Jan 22 '13 at 16:57
3

Check out direnv

direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.

Basically checks for .envrc files located in the current or parent directories, and adjusts your environment variables accordingly.

d_coder
  • 1,131
  • 13
  • 18