在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)。
当前回答
有时候,前面所有的答案都不适用。如果你想在Eclipse或IntelliJ IDEA中访问一个系统变量(如M2_HOME),在这种情况下,唯一适合我的是:
首先(步骤1)编辑/etc/launchd.conf以包含如下一行:“setenv VAR value”,然后(步骤2)重新启动。
简单地修改.bash_profile将不起作用,因为在OS X中应用程序不会像在其他Unix中那样启动;它们不继承父函数的壳变量。其他的修改都不管用了,原因我也不知道。也许有人可以解释一下。
其他回答
除了建议的方法之外,还有一件事需要注意,至少在OS X 10.5 (Leopard)中,launchd.conf中设置的变量将与.profile中的设置合并。我认为这可能对~/. macosx /environment中的设置有效。plist也是,但我还没有验证。
任何Bash启动文件—~/。bashrc,(~ /。bash_profile、~ / . profile。还有一些奇怪的文件,名为~/. macosx /environment。用于GUI应用程序中的环境变量。
做的事: 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专用工具)的设置放到单独的脚本中。
在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。