在OS X中修改环境变量如PATH的正确方法是什么?

我看了谷歌一点,找到了三个不同的文件进行编辑:

/etc/paths ~ / . profile ~ / tcshrc

我甚至没有这些文件中的一些,我很确定.tcshrc是错误的,因为OS X现在使用bash。这些变量,特别是PATH,定义在哪里?

我运行的是OS X v10.5 (Leopard)。


当前回答

iOS上的所有神奇之处都是使用source和文件,在这里导出环境变量。

例如:

你可以像这样创建一个文件:

export bim=fooo
export bom=bar

保存为bimbom。Env,然后做source ./bimbom.ev。 Voilá,你有你的环境变量。

检查它们:

echo $bim

其他回答

做的事: vim ~ / . bash_profile 文件可能不存在(如果不存在,您可以直接创建它)。 输入并保存文件: 导出路径= $路径:YOUR_PATH_HERE 运行 源~ / . bash_profile

很简单:

编辑~ /。对变量进行配置并按如下方式放置

$ vim ~/.profile

在文件中放置:

MY_ENV_VAR =值

保存(:wq) 重启终端(退出并重新打开) 确保这一切都没问题:

$MY_ENV_VAR 美元的价值


对于单个用户修改,使用~/。你列出的人的简介。下面的链接解释了Bash何时读取不同的文件。

http://telin.ugent.be/~slippens/drupal/bashrc_and_others

如果你想为gui应用程序设置环境变量,你需要~/. macosx /environment。plist文件

直到和包括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。

就像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.