在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)。
当前回答
任何Bash启动文件—~/。bashrc,(~ /。bash_profile、~ / . profile。还有一些奇怪的文件,名为~/. macosx /environment。用于GUI应用程序中的环境变量。
其他回答
简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:
touch .bash_profile
then
open -a TextEdit.app .bash_profile
add
export TOMCAT_HOME=/Library/Tomcat/Home
保存文档,您就完成了。
虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在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.
不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。
$PATH变量也受制于path_helper,它反过来利用/etc/paths文件和/etc/paths.d中的文件
更详细的描述可以在PATH和Leopard(2008-11)的其他环境问题中找到。
iOS上的所有神奇之处都是使用source和文件,在这里导出环境变量。
例如:
你可以像这样创建一个文件:
export bim=fooo
export bom=bar
保存为bimbom。Env,然后做source ./bimbom.ev。 Voilá,你有你的环境变量。
检查它们:
echo $bim
来自单一来源的命令行和GUI应用程序的解决方案(适用于Mac OS X v10.10 (Yosemite)和Mac OS X v10.11 (El Capitan))
让我们假设在~/中有环境变量定义。Bash_profile如下所示:
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
export GOPATH="$HOME/go"
export PATH="$PATH:/usr/local/opt/go/libexec/bin:$GOPATH/bin"
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
我们需要一个启动代理,它将在每次登录时运行,并随时按需将这些变量加载到用户会话。我们还需要一个shell脚本来解析这些定义并构建由代理执行的必要命令。
在~/Library/LaunchAgents/目录下创建一个后缀为plist的文件(例如osx-env-sync.plist),内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>osx-env-sync</string>
<key>ProgramArguments</key>
<array>
<string>bash</string>
<string>-l</string>
<string>-c</string>
<string>
$HOME/.osx-env-sync.sh
</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
-l参数在这里很重要;使用登录shell执行shell脚本是必要的,以便~/。Bash_profile是在执行脚本之前首先获取的。
现在,shell脚本。在~/.osx-env-sync.sh中创建以下内容:
grep export $HOME/.bash_profile | while IFS=' =' read ignoreexport envvar ignorevalue; do
launchctl setenv "${envvar}" "${!envvar}"
done
确保shell脚本是可执行的:
chmod +x ~/.osx-env-sync.sh
现在,加载当前会话的启动代理:
launchctl load ~/Library/LaunchAgents/osx-env-sync.plist
(重新)启动一个GUI应用程序并验证它可以读取环境变量。
设置是持久的。它将在重新启动和重新登录后仍然有效。
在初始设置之后(您刚刚完成了),如果您想在~/中反映任何更改。Bash_profile再次到您的整个环境,重新运行launchctl load…命令不会执行你想要的;相反,你会得到如下警告:
< $ HOME > /图书馆/ LaunchAgents / osx-env-sync。plist:操作已经在进行中
为了在不退出/登录的情况下重新加载环境变量,请执行以下操作:
launchctl unload ~/Library/LaunchAgents/osx-env-sync.plist
launchctl load ~/Library/LaunchAgents/osx-env-sync.plist
最后,确保重新启动已经运行的应用程序(包括Terminal.app),让它们知道这些更改。
我还将这里的代码和解释推送到GitHub项目:osx-env-sync。
我希望这将是最终的解决方案,至少对于最新版本的OS X (Yosemite & El Capitan)来说是这样。