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

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

/etc/paths ~ / . profile ~ / tcshrc

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

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


当前回答

布鲁诺正在正确的轨道上。我已经做了大量的研究,如果你想设置在所有GUI应用程序中可用的变量,你唯一的选择是/etc/launchd.conf。

请注意环境。plist不适用于通过Spotlight启动的应用程序。这是由史蒂夫·塞克斯顿记录的。

Open a terminal prompt Type sudo vi /etc/launchd.conf (note: this file might not yet exist) Put contents like the following into the file # Set environment variables here so they are available globally to all apps # (and Terminal), including those launched via Spotlight. # # After editing this file run the following command from the terminal to update # environment variables globally without needing to reboot. # NOTE: You will still need to restart the relevant application (including # Terminal) to pick up the changes! # grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl # # See http://www.digitaledgesw.com/node/31 # and http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/ # # Note that you must hardcode the paths below, don't use environment variables. # You also need to surround multiple values in quotes, see MAVEN_OPTS example below. # setenv JAVA_VERSION 1.6 setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home setenv GROOVY_HOME /Applications/Dev/groovy setenv GRAILS_HOME /Applications/Dev/grails setenv NEXUS_HOME /Applications/Dev/nexus/nexus-webapp setenv JRUBY_HOME /Applications/Dev/jruby setenv ANT_HOME /Applications/Dev/apache-ant setenv ANT_OPTS -Xmx512M setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m" setenv M2_HOME /Applications/Dev/apache-maven setenv JMETER_HOME /Applications/Dev/jakarta-jmeter Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above. Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ IDEA and other GUI applications you launch via Spotlight.

其他回答

我的个人实践是.bash_profile。我在这里添加路径并附加到Path变量,

GOPATH=/usr/local/go/bin/
MYSQLPATH=/usr/local/opt/mysql@5.6/bin

PATH=$PATH:$GOPATH:$MYSQLPATH

之后,我可以有个人的路径通过echo$ GOPATH, echo$MYSQLPATH或所有通过echo$ Path。

有时候,前面所有的答案都不适用。如果你想在Eclipse或IntelliJ IDEA中访问一个系统变量(如M2_HOME),在这种情况下,唯一适合我的是:

首先(步骤1)编辑/etc/launchd.conf以包含如下一行:“setenv VAR value”,然后(步骤2)重新启动。

简单地修改.bash_profile将不起作用,因为在OS X中应用程序不会像在其他Unix中那样启动;它们不继承父函数的壳变量。其他的修改都不管用了,原因我也不知道。也许有人可以解释一下。

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

对于Bash,尝试将环境变量添加到文件/etc/profile中,以使它们对所有用户可用。不需要重新启动,只需启动一个新的终端会话。

2月2022日(MacOs 12+)

这里的解决方案应该在重新启动或应用程序重新启动后工作。

CLI

打开所选配置文件的CLI。

为bash打开~/.bash_profile 为zsh打开~/.zshrc

添加(或替换)

export varName=varValue 

(如果varValue中有空格-将其包装成")

确保重新启动命令行应用程序。

GUI

完成CLI步骤。 确保GUI应用程序关闭。 从命令行打开GUI应用程序。例如:

open /Applications/Sourcetree.app

(你也可以在.zshrc文件中使用这个命令的别名)

原则

Mac没有为所有上下文设置环境变量的配置选项。 避免更改用户配置文件之外的任何内容。

已经不管用了

(MacOS 12.1+) 编辑/etc/launchd.conf 带有plist后缀的XML文件