Set environmental variables in a particular directory

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$ 

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


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.