在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)。
当前回答
来自单一来源的命令行和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)来说是这样。
其他回答
简单又快速地做了这个。首先创建一个~/。Bash_profile来自终端:
touch .bash_profile
then
open -a TextEdit.app .bash_profile
add
export TOMCAT_HOME=/Library/Tomcat/Home
保存文档,您就完成了。
做的事: vim ~ / . bash_profile 文件可能不存在(如果不存在,您可以直接创建它)。 输入并保存文件: 导出路径= $路径:YOUR_PATH_HERE 运行 源~ / . bash_profile
在处理时,基本上有两个问题需要解决 第一个是在调用程序时 来自Spotlight (Mac右侧的放大镜图标) 菜单/状态栏),第二个是从Dock调用程序。 从终端应用程序/实用程序调用程序非常简单 因为它从标准shell位置读取环境 (~ /。简介~ /。bash_profile、~ /。bashrc,(等等)。
当从Dock调用程序时,使用~/.MacOSX/environment.plist <dict>元素包含序列 <键>键< /关键> <字符串> < /字符串>价值元素。
当从Spotlight调用程序时,确保launchd已经启动 设置所需的所有键/值设置。
To solve both problems simultaneously, I use a login item (set via the System Preferences tool) on my User account. The login item is a bash script that invokes an Emacs lisp function although one can of course use their favorite scripting tool to accomplish the same thing. This approach has the added benefit that it works at any time and does not require a reboot, i.e. one can edit ~/.profile, run the login item in some shell and have the changes visible for newly invoked programs, from either the Dock or Spotlight.
细节:
登录项:~/bin/macosx-startup
#!/bin/bash
bash -l -c "/Applications/Emacs.app/Contents/MacOS/Emacs --batch -l ~/lib/emacs/elisp/macosx/environment-support.el -f generate-environment"
Emacs的lisp功能:~/lib/ Emacs /elisp/macosx/ environment -support.el
;;; Provide support for the environment on Mac OS X
(defun generate-environment ()
"Dump the current environment into the ~/.MacOSX/environment.plist file."
;; The system environment is found in the global variable:
;; 'initial-environment' as a list of "KEY=VALUE" pairs.
(let ((list initial-environment)
pair start command key value)
;; clear out the current environment settings
(find-file "~/.MacOSX/environment.plist")
(goto-char (point-min))
(setq start (search-forward "<dict>\n"))
(search-forward "</dict>")
(beginning-of-line)
(delete-region start (point))
(while list
(setq pair (split-string (car list) "=")
list (cdr list))
(setq key (nth 0 pair)
value (nth 1 pair))
(insert " <key>" key "</key>\n")
(insert " <string>" value "</string>\n")
;; Enable this variable in launchd
(setq command (format "launchctl setenv %s \"%s\"" key value))
(shell-command command))
;; Save the buffer.
(save-buffer)))
注意:这个解决方案是在我添加我的解决方案之前的一个混合体,特别是由Matt Curtis提供的解决方案,但我故意试图保持我的~/。bash_profile独立于内容平台,并将launchd环境(Mac专用工具)的设置放到单独的脚本中。
不要期望~/.launchd.conf能够工作
launchctl的手册页说它从来没有工作过:
已弃用和删除的功能 Launchctl不再具有交互模式,也不再接受来自stdin的命令。/etc/launchd.conf文件不再用于在早期启动时运行子命令;出于安全考虑,该功能已被删除。虽然有文档表明在设置用户会话之前会查询$HOME/.launchd.conf,但这个功能从未实现过。
如何为Spotlight启动的新进程设置环境(不需要重新启动)
您可以使用launchctl setenv设置launchd(以及从Spotlight开始的任何东西)所使用的环境。例如,设置路径:
launchctl setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
或者如果你想在。bashrc或类似文件中设置你的路径,那么在launchd中镜像它:
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
launchctl setenv PATH $PATH
没有必要重新启动,但如果你想让应用程序接受改变的环境,就需要重新启动应用程序。
这包括已经在终端下运行的任何shell。如果你在那里,你可以更直接地设置环境,例如export PATH=/opt/local/bin:/opt/local/sbin:$PATH for bash或zsh。
重启后如何保持更改
新方法(自10.10 Yosemite起)
使用launchctl config用户路径/bin:/usr/bin:/mystuff。更多信息请参见man launchctl。
以前的方法
这个答案顶部的launchctl手册页引用说,出于安全原因,这里描述的特性(在引导时读取/etc/launchd.conf)被删除了,因此ymmv. conf。
为了在重启后保持更改,你可以在/etc/launchd.conf中设置环境变量,如下所示:
setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
重启时自动执行Launchd.conf。
如果你想让这些改变现在生效,你应该使用这个命令重新处理launchd.conf(感谢@mklement的提示!)
egrep -v '^\s*#' /etc/launchd.conf | launchctl
你可以通过命令man launchctl找到更多关于launchctl以及它如何加载launchd.conf的信息。
登录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文件中是一个好主意。