在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
在OS X中修改环境变量如PATH的正确方法是什么?
我看了谷歌一点,找到了三个不同的文件进行编辑:
/etc/paths ~ / . profile ~ / tcshrc
我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?
我运行的是OS X v10.5 (Leopard)。
当前回答
登录shell
/etc/profile
shell首先执行/etc/profile.文件中的命令具有根权限的用户可以设置这个文件,为运行Bash的用户建立系统范围内的默认特征。
.bash_profile
.bash_login
.profile
接下来shell查找~/。bash_profile、~ /。Bash_login和~/。配置文件(~/是您的主目录的简写),按此顺序执行它找到的第一个文件中的命令。您可以在其中一个文件中放置命令来覆盖/etc/profile中设置的默认值在虚拟终端上运行的shell不会执行这些文件中的命令。
.bash_logout
注销时,bash执行~/. conf中的命令。bash_logout文件。这个文件通常保存在会话结束后清理的命令,比如删除临时文件的命令。
交互式非登录shell
/etc/bashrc
虽然不是由bash直接调用,但许多~/。Bashrc文件调用/etc/bashrc.这种设置允许使用根权限的用户为非登录bash shell建立系统范围内的默认特征。
.bashrc
交互式非登录shell在~/. shell中执行命令。bashrc文件。(通常,登录shell的启动文件(如.bash_profile)将运行此文件,因此登录和非登录shell都将运行.bashrc中的命令。
因为.bashrc中的命令可能会被执行多次,并且子shell继承导出的变量,所以将添加到现有变量的命令放在.bash_profile文件中是一个好主意。
其他回答
直到和包括OS X v10.7 (Lion),你可以设置它们:
~/.MacOSX/environment.plist
See:
https://developer.apple.com/legacy/library/qa/qa1067/_index.html https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html
对于终端中的PATH,您应该能够在.bash_profile或.profile中设置(尽管您可能必须创建它)
对于OS X v10.8 (Mountain Lion)及以上版本,您需要使用launchd和launchctl。
任何Bash启动文件—~/。bashrc,(~ /。bash_profile、~ / . profile。还有一些奇怪的文件,名为~/. macosx /environment。用于GUI应用程序中的环境变量。
在追逐环境变量首选项窗格后,发现链接已经损坏,在苹果网站上搜索似乎表明他们已经忘记了它……我开始回到难以捉摸的发射过程。
在我的系统(Mac OS X 10.6.8)上,似乎在环境中定义的变量。plist被可靠地导出到从Spotlight启动的应用程序(通过launchd)。我的问题是,这些变量没有被导出到终端中的新bash会话。也就是说,我的问题与这里描述的相反。
注:环境。如前所述,plist看起来像JSON,而不是XML
我可以通过编辑~/MacOSX/environment让Spotlight应用程序看到变量。plist和 我能够通过在我的.profile文件中添加以下内容将相同的vars强制到一个新的Terminal会话中:
eval $(launchctl export)
就像Matt Curtis给出的答案一样,我通过launchctl设置环境变量,但我将它包装在一个名为export的函数中,因此每当我像在.bash_profile中一样导出一个变量时,它也由launchctl设置。我是这样做的:
My .bash_profile consists solely of one line, (This is just personal preference.) source .bashrc My .bashrc has this: function export() { builtin export "$@" if [[ ${#@} -eq 1 && "${@//[^=]/}" ]] then launchctl setenv "${@%%=*}" "${@#*=}" elif [[ ! "${@//[^ ]/}" ]] then launchctl setenv "${@}" "${!@}" fi } export -f export The above will overload the Bash builtin "export" and will export everything normally (you'll notice I export "export" with it!), then properly set them for OS X app environments via launchctl, whether you use any of the following: export LC_CTYPE=en_US.UTF-8 # ~$ launchctl getenv LC_CTYPE # en_US.UTF-8 PATH="/usr/local/bin:${PATH}" PATH="/usr/local/opt/coreutils/libexec/gnubin:${PATH}" export PATH # ~$ launchctl getenv PATH # /usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin export CXX_FLAGS="-mmacosx-version-min=10.9" # ~$ launchctl getenv CXX_FLAGS # -mmacosx-version-min=10.9 This way I don't have to send every variable to launchctl every time, and I can just have my .bash_profile / .bashrc set up the way I want. Open a terminal window, check out your environment variables you're interested in with launchctl getenv myVar, change something in your .bash_profile/.bashrc, close the terminal window and re-open it, check the variable again with launchctl, and voilá, it's changed. Again, like the other solutions for the post-Mountain Lion world, for any new environment variables to be available for apps, you need to launch or re-launch them after the change.
对于单个用户修改,使用~/。你列出的人的简介。下面的链接解释了Bash何时读取不同的文件。
http://telin.ugent.be/~slippens/drupal/bashrc_and_others
如果你想为gui应用程序设置环境变量,你需要~/. macosx /environment。plist文件