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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

很简单:

编辑~ /。对变量进行配置并按如下方式放置

$ vim ~/.profile

在文件中放置:

MY_ENV_VAR =值

保存(:wq) 重启终端(退出并重新打开) 确保这一切都没问题:

$MY_ENV_VAR 美元的价值


其他回答

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

例如:

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

export bim=fooo
export bom=bar

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

检查它们:

echo $bim

直到和包括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。

不要期望~/.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的信息。

在处理时,基本上有两个问题需要解决 第一个是在调用程序时 来自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专用工具)的设置放到单独的脚本中。

虽然这里的答案并不是“错误的”,但我还要补充一点:永远不要在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.

不管“蹩脚”与否,如果您将这些更改限制在较小的范围内,您将拥有一个更加稳定的系统。