Directory based environment variable scope - how t

2019-03-14 11:27发布

I have a set of tools which I need to pass parameters depending on the project I'm working on. I'd like to be able to automatically set a couple of environment variables based on the current directory. So when I switched between directories, my commonly used env vars would also change. Example:

Let's current directory is foo, thus if I do:

~/foo$ ./myscript --var1=$VAR1

VAR1 would have some foo based value.

Then, let's say I switched to bar directory. If I do:

~/bar$ ./myscript --var1=$VAR1

VAR1 should now have some bar based value.

Is that possible? How?

7条回答
我想做一个坏孩纸
2楼-- · 2019-03-14 12:09

This is not something that is directly supported with the built-in features of bash or any other common shell. However, you can create your own "cd" command that will do whatever you want. For example, you could alias cd to do the cd and then run a special script (eg: ~/bin/oncd). That script could look up the new directory in a database and run some commands, or see if there's a special file (eg: .env) in the directory and load it, etc.

查看更多
登录 后发表回答