Setting environment variables on OS X

后端 未结 30 3220
你的背包
你的背包 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:55

    I think the OP is looking for a simple, Windows-like solution.

    Here you go:

    http://www.apple.com/downloads/macosx/system_disk_utilities/environmentvariablepreferencepane.html

    0 讨论(0)
  • 2020-11-21 05:58

    Well, I'm unsure about the /etc/paths and ~/.MacOSX/environment.plist files. Those are new.

    But with Bash, you should know that .bashrc is executed with every new shell invocation and .bash_profile is only executed once at startup.

    I don't know how often this is with Mac OS X. I think the distinction has broken down with the window system launching everything.

    Personally, I eliminate the confusion by creating a .bashrc file with everything I need and then do:

    ln -s .bashrc .bash_profile
    
    0 讨论(0)
  • 2020-11-21 05:58

    For Bash, try adding your environment variables to the file /etc/profile to make them available for all users. No need to reboot, just start a new Terminal session.

    0 讨论(0)
  • 2020-11-21 05:59

    There are essentially two problems to solve when dealing with environment variables in OS X. The first is when invoking programs from Spotlight (the magnifying glass icon on the right side of the Mac menu/status bar) and the second when invoking programs from the Dock. Invoking programs from a Terminal application/utility is trivial because it reads the environment from the standard shell locations (~/.profile, ~/.bash_profile, ~/.bashrc, etc.)

    When invoking programs from the Dock, use ~/.MacOSX/environment.plist where the <dict> element contains a sequence of <key>KEY</key><string>theValue</string> elements.

    When invoking programs from Spotlight, ensure that launchd has been setup with all the key/value settings you require.

    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.

    Details:

    Login item: ~/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 function: ~/lib/emacs/elisp/macosx/envionment-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)))
    

    NOTE: This solution is an amalgam of those coming before I added mine, particularly that offered by Matt Curtis, but I have deliberately tried to keep my ~/.bash_profile content platform independent and put the setting of the launchd environment (a Mac only facility) into a separate script.

    0 讨论(0)
  • 2020-11-21 05:59

    All the magic on iOS only goes with using source with the file, where you export your environment variables.

    For example:

    You can create an file like this:

    export bim=fooo
    export bom=bar
    

    Save this file as bimbom.env, and do source ./bimbom.ev. Voilá, you got your environment variables.

    Check them with:

    echo $bim
    
    0 讨论(0)
  • 2020-11-21 06:02

    There are two type of shells at play here.

    • Non-login: .bashrc is reloaded every time you start a new copy of Bash
    • Login: The .profile is loaded only when you either login, or explicitly tell Bash to load it and use it as a login shell.

    It's important to understand here that with Bash, file .bashrc is only read by a shell that's both interactive and non-login, and you will find that people often load .bashrc in .bash_profile to overcome this limitation.

    Now that you have the basic understanding, let’s move on to how I would advice you to set it up.

    • .profile: create it non-existing. Put your PATH setup in there.
    • .bashrc: create if non-existing. Put all your aliases and custom methods in there.
    • .bash_profile: create if non-existing. Put the following in there.

    .bash_file:

    #!/bin/bash
    source ~/.profile # Get the PATH settings
    source ~/.bashrc  # Get Aliases and Functions
    #
    
    0 讨论(0)
提交回复
热议问题