views:

38

answers:

2

If I have 'export BLA="hey there"' in .profile in my home directory, how can I change this later in other directories just by cd into the other directory? Also, is there a way to just set a new environment variable when I cd into a directory.

My first attempt was to just make another .bashrc file in the directory where I want the change but apparently that proved less than effective.

I'm on OS X btw.

+2  A: 

Why do you need to do this? Just curious.

But you can override cd and do some extra processing in your .bashrc:

cd() {
    builtin cd "$@"
    if [[ `pwd` == '/path/to/dir' ]]; then
        export VAR=blah
    ]]
}

After you add that, don't forget to start a new bash shell or source it via:

source ~/.bashrc
xyld
The reason I want to do this is to have different values for variables for different rails apps. The different apps might use the same variables but they need values specific to them.
kjs3
@kjs3 please mark my answer with a checkmark if you deem it to be "correct" or that it helped you achieve your goal. Thanks!
xyld
A: 

Building on xyld's answer, this lets you do the scary .bashrc-in-any-directory thing. I'm pretty sure this is a really terrible idea:

cd() {
    builtin cd "$@"
    if [[ -e `pwd`/.supplemental-bashrc ]]; then
        source `pwd`/.supplemental-bashrc
    fi
}
pkh
Hah, yeah, you could definitely do that. Definitely quite scary though
xyld