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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

我认为OP正在寻找一个简单的,类似windows的解决方案。

给你:

http://www.apple.com/downloads/macosx/system_disk_utilities/environmentvariablepreferencepane.html

其他回答

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

例如:

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

export bim=fooo
export bom=bar

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

检查它们:

echo $bim

/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 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应用程序中的环境变量。

$PATH变量也受制于path_helper,它反过来利用/etc/paths文件和/etc/paths.d中的文件

更详细的描述可以在PATH和Leopard(2008-11)的其他环境问题中找到。