在特定目录中设置环境变量 [英] Set environmental variables in a particular directory
问题描述
这是一个.bashrc问题.我想在诸如 .rvmrc
之类的特定目录中为.bashrc设置"export FOO = bar"
.
我在下面尝试过
$ touch〜/foo/.bashrc$ echo'export RAILS_ENV = development'>>〜/foo/.bashrc$ cd foo/$ env | grep RAILS_ENV
但是在这种情况下, RAILS_ENV
不应设置任何内容.
如果我设置为 .rvmrc
而不是.bashrc,它将通过!但这是设置为 .bashrc
的更好方法,因为我不需要安装rvm环境.
有解决方案吗?
在您的bashrc中的设置为:
PROMPT_COMMAND ='[[$ PWD =="/foo/bar/"]]&&出口FOO = BAR ||未设置FOO'
每次对提示进行重写时(实际上是在实际写入提示之前),都会执行PROMPT_COMMAND变量的内容,上面的命令针对"/foo/bar"来检查$ PWD变量(保存您的shell的当前工作目录).如果匹配,则导出您的变量(如果不匹配),则该变量未设置.
EG
peteches @ yog-sothoth $ PROMPT_COMMAND ='[[$ PWD =="/home/peteches/test"]]&&出口FOO = BAR ||未设置FOO'peteches @ yog-sothoth $ pwd/home/petechespeteches @ yog-sothoth $ cd测试peteches @ yog-sothoth $ pwd/home/peteches/testpeteches @ yog-sothoth $ env |grep FOO6:FOO = BAR73:PROMPT_COMMAND = [[$ PWD =="/home/peteches/test"]]&&出口FOO = BAR ||未设置FOOpeteches @ yog-sothoth $ cd ../peteches @ yog-sothoth $ pwd/home/petechespeteches @ yog-sothoth $ env |grep FOO72:PROMPT_COMMAND = [[$ PWD =="/home/peteches/test"]]&&出口FOO = BAR ||未设置FOOpeteches @ yog-sothoth $
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?
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$
这篇关于在特定目录中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!