在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)。
当前回答
在Mac OS上设置PATH环境变量
打开终端程序(默认在应用程序/实用程序文件夹中)。运行以下命令
touch ~/.bash_profile; open ~/.bash_profile
这将在默认文本编辑器中打开文件。
以Android SDK为例:
你需要添加路径到你的Android SDK平台工具和工具目录。在我的例子中,我将使用“/Development/android-sdk-macosx”作为SDK的安装目录。增加如下一行:
export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
保存文件并退出文本编辑器。执行.bash_profile更新PATH:
source ~/.bash_profile
现在,每次你打开终端程序,你的路径将包括Android SDK。
其他回答
这很简单。编辑文件.profile (vi, nano, Sublime Text或其他文本编辑器)文件。你可以在~/目录(用户目录)中找到它,并像这样设置:
export MY_VAR=[your value here]
Java home的示例:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
保存它并返回到终端。
你可以用:
source .profile
或者关闭并打开终端窗口。
任何Bash启动文件—~/。bashrc,(~ /。bash_profile、~ / . profile。还有一些奇怪的文件,名为~/. macosx /environment。用于GUI应用程序中的环境变量。
对于单个用户修改,使用~/。你列出的人的简介。下面的链接解释了Bash何时读取不同的文件。
http://telin.ugent.be/~slippens/drupal/bashrc_and_others
如果你想为gui应用程序设置环境变量,你需要~/. macosx /environment。plist文件
/etc/launchd.conf在OS X v10.10 (Yosemite)、OS X v10.11 (El Capitan)、macOS v10.12 (Sierra)或macOS v10.13 (High Sierra)中不使用。
从launchctl手册页:
/etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;
this functionality was removed for security considerations.
这个Ask Different answer中描述的方法适用于我(重启后):从Dock或Spotlight启动的应用程序继承了我在~/Library/LaunchAgents/my.startup.plist中设置的环境变量。(在我的例子中,我需要将LANG设置为en_US。UTF-8,用于Sublime Text插件。)
虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在OS X中改变影响“所有进程”的环境变量,甚至在shell之外,也不要影响给定用户交互运行的所有进程。
In my experience, global changes to environment variables like PATH for all processes are even more likely to break things on OS X than on Windows. Reason being, lots of OS X applications and other software (including, perhaps especially, components of the OS itself) rely on UNIX command-line tools under the hood, and assume the behavior of the versions of these tools provided with the system, and don't necessarily use absolute paths when doing so (similar comments apply to dynamically-loaded libraries and DYLD_* environment variables). Consider, for instance, that the highest-rated answers to various Stack Overflow questions about replacing OS X-supplied versions of interpreters like Python and Ruby generally say "don't do this."
OS X is really no different than other UNIX-like operating systems (e.g., Linux, FreeBSD, and Solaris) in this respect; the most likely reason Apple doesn't provide an easy way to do this is because it breaks things. To the extent Windows isn't as prone to these problems, it's due to two things: (1) Windows software doesn't tend to rely on command-line tools to the extent that UNIX software does, and (2) Microsoft has had such an extensive history of both "DLL hell" and security problems caused by changes that affect all processes that they've changed the behavior of dynamic loading in newer Windows versions to limit the impact of "global" configuration options like PATH.
不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。