How can I set environment variables depending on directory
If I have 'export BL开发者_运维知识库A="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.
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
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
}
精彩评论