Setting environment variables on OS X

后端 未结 30 3789
你的背包
你的背包 2020-11-21 05:15

What is the proper way to modify environment variables like PATH in OS X?

I\'ve looked on Google a little bit and found three different files to edit:

30条回答
  •  不要未来只要你来
    2020-11-21 05:38

    Don't expect ~/.launchd.conf to work

    The man page for launchctl says that it never worked:

    DEPRECATED AND REMOVED FUNCTIONALITY

    launchctl no longer has an interactive mode, nor does it accept commands from stdin. The /etc/launchd.conf file is no longer consulted for subcommands to run during early boot time; this functionality was removed for security considerations. While it was documented that $HOME/.launchd.conf would be consulted prior to setting up a user's session, this functionality was never implemented.

    How to set the environment for new processes started by Spotlight (without needing to reboot)

    You can set the environment used by launchd (and, by extension, anything started from Spotlight) with launchctl setenv. For example to set the path:

    launchctl setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
    

    Or if you want to set up your path in .bashrc or similar, then have it mirrored in launchd:

    PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
    launchctl setenv PATH $PATH
    

    There's no need to reboot though you will need to restart an app if you want it to pick up the changed environment.

    This includes any shells already running under Terminal.app, although if you're there you can set the environment more directly, e.g. with export PATH=/opt/local/bin:/opt/local/sbin:$PATH for bash or zsh.

    How to keeping changes after a reboot

    New method (since 10.10 Yosemite)

    Use launchctl config user path /bin:/usr/bin:/mystuff. See man launchctl for more information.

    Previous method

    The launchctl man page quote at the top of this answer says the feature described here (reading /etc/launchd.conf at boot) was removed for security reasons, so ymmv.

    To keep changes after a reboot you can set the environment variables from /etc/launchd.conf, like so:

    setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
    

    launchd.conf is executed automatically when you reboot.

    If you want these changes to take effect now, you should use this command to reprocess launchd.conf (thanks @mklement for the tip!)

    egrep -v '^\s*#' /etc/launchd.conf | launchctl
    

    You can find out more about launchctl and how it loads launchd.conf with the command man launchctl.

提交回复
热议问题