在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应用程序中的环境变量。
其他回答
做的事: 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专用工具)的设置放到单独的脚本中。
$PATH变量也受制于path_helper,它反过来利用/etc/paths文件和/etc/paths.d中的文件
更详细的描述可以在PATH和Leopard(2008-11)的其他环境问题中找到。
任何Bash启动文件—~/。bashrc,(~ /。bash_profile、~ / . profile。还有一些奇怪的文件,名为~/. macosx /environment。用于GUI应用程序中的环境变量。
更新(2017-08-04)
从macOS 10.12.6 (Sierra)开始,这个方法似乎已经停止在Apache httpd上工作(对于系统和launchctl配置的用户选项)。其他项目似乎没有受到影响。可以想象,这是httpd中的一个bug。
原来的答案
这涉及到OS X 10.10+(特别是10.11+由于无根模式,其中/usr/bin不再可写)。
我在很多地方读到过,使用launchctl setenv PATH <new PATH >来设置PATH变量由于OS X中的一个错误而不起作用(从个人经验来看似乎是正确的)。我发现还有另一种方法可以为非shell启动的应用程序设置PATH:
sudo launchctl config user path <new path>
这个选项记录在launchctl手册页中:
config system | user parameter value Sets persistent configuration information for launchd(8) domains. Only the system domain and user domains may be configured. The location of the persistent storage is an implementation detail, and changes to that storage should only be made through this subcommand. A reboot is required for changes made through this subcommand to take effect. [...] path Sets the PATH environment variable for all services within the target domain to the string value. The string value should conform to the format outlined for the PATH environment variable in environ(7). Note that if a service specifies its own PATH, the service-specific environment variable will take precedence. NOTE: This facility cannot be used to set general environment variables for all services within the domain. It is intentionally scoped to the PATH environment vari- able and nothing else for security reasons.
我已经确认这与从Finder(使用getenv获取PATH)开始的GUI应用程序一起工作。 请注意,您只需要这样做一次,并且更改将在重新启动时保持不变。