macos - 在 OS X 上设置环境变量

标签 macos bash path environment-variables

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

我在 Google 上搜索了一下,找到了三个不同的文件来编辑:

  • /etc/路径
  • ~/.profile
  • ~/.tcshrc

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

我在跑 OS X v10.5 (豹)。

最佳答案

Bruno 正走在正轨上。我进行了广泛的研究,如果您想设置所有 GUI 应用程序中可用的变量,您唯一的选择是 /etc/launchd.conf

请注意environment.plist does not work for applications launched via Spotlight. This is documented by Steve Sexton here .

  1. 打开终端提示符

  2. 键入 sudo vi/etc/launchd.conf(注意:此文件可能尚不存在)

  3. 将如下内容放入文件中

    # 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
    
  4. 保存您在 vi 中的更改并重新启动您的 Mac。或者使用 grep/xargs上面代码注释中显示的命令。

  5. 通过打开终端窗口并键入 export 证明您的变量正在工作,您应该会看到新变量。这些也将在 IntelliJ IDEA 和您通过 Spotlight 启动的其他 GUI 应用程序中提供。

关于macos - 在 OS X 上设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/135688/

相关文章:

linux - 添加一个用户并为许多服务器linux给他相同的密码

python - 从python中的命令行参数获取文件路径

macos - Mac OS X 应用程序要使用哪些控件?

macos - 如何在 NSSearchField 中自定义光标?

linux - 关闭窗口时不写入 Bash 命令历史记录

c# - 绝对到相对路径

sql - FOR XML 路径打开/关闭

objective-c - 在 OS X Lion 中,向 Finder 询问当前选择,返回错误的文件

xcode - 我如何验证死代码是否已从二进制文件中删除?

bash - sudo和chpasswd在一行上