在OS X上设置环境变量

发布于 2021-01-29 19:36:42

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

我稍微看了一下Google,发现需要编辑三个不同的文件:

  • /etc/paths
  • 〜/ .profile
  • 〜/ .tcshrc

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

我正在运行OS X v10.5(Leopard)。

关注者
0
被浏览
68
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    布鲁诺是正确的。我已经进行了广泛的研究,如果您想设置所有GUI应用程序中都可用的变量,则唯一的选择是/etc/launchd.conf

    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
    
    1. 在vi中保存所做的更改,然后重新启动Mac。或使用上面的代码注释中显示的grep/xargs命令。

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看