Set environment variables on Mac OS X Lion

后端 未结 16 1225
余生分开走
余生分开走 2020-11-22 11:01

When someone says \"edit your .plist file\" or \"your .profile\" or \".bash_profile\" etc, this just confuses me. I have no idea where these files are, how to create them if

相关标签:
16条回答
  • 2020-11-22 11:23

    Open Terminal:

    vi ~/.bash_profile
    

    Apply changing to system (no need restart computer):

    source ~/.bash_profile
    

    (Also work with macOS Sierra 10.12.1)

    0 讨论(0)
  • 2020-11-22 11:23

    It is recommended to check default terminal shell before setting any environment variables, via following commands:

    $ echo $SHELL
    /bin/zsh
    

    If your default terminal is /bin/zsh (Z Shell) like in my case (Personally prefer Z Shell), then you should set these environment variable in ~/.zshenv file with following contents (In this example, setting JAVA_HOME environment variable, but same applies to others):

    export JAVA_HOME="$(/usr/libexec/java_home)"
    

    Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.

    0 讨论(0)
  • 2020-11-22 11:25

    Simplified Explanation

    This post/question is kind of old, so I will answer a simplified version for OS X Lion users. By default, OSX Lion does not have any of the following files:

    • ~/.bashrc
    • ~/.bash_profile
    • ~/.profile

    At most, if you've done anything in the terminal you might see ~/.bash_history

    What It Means

    You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:

    1. %> emacs .profile
    2. [from w/in emacs type:] source ~/.bashrc
    3. [from w/in emacs type:] Ctrl + x Ctrl + s (to save the file)
    4. [from w/in emacs type:] Ctrl + x Ctrl + c (to close emacs)
    5. %> emacs .bashrc
    6. [from w/in emacs type/paste all your bash commands, save, and exit]

    The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases and put all your alias commands in ~/.bash_aliases.

    0 讨论(0)
  • 2020-11-22 11:27

    Unfortunately none of these answers solved the specific problem I had.

    Here's a simple solution without having to mess with bash. In my case, it was getting gradle to work (for Android Studio).

    Btw, These steps relate to OSX (Mountain Lion 10.8.5)

    • Open up Terminal.
    • Run the following command:

      sudo nano /etc/paths (or sudo vim /etc/paths for vim)

      nano

    • Go to the bottom of the file, and enter the path you wish to add.
    • Hit control-x to quit.
    • Enter 'Y' to save the modified buffer.
    • Open a new terminal window then type:

      echo $PATH

    You should see the new path appended to the end of the PATH

    I got these details from this post:

    http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q

    I hope that can help someone else

    0 讨论(0)
  • 2020-11-22 11:29

    Your .profile or .bash_profile are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane, you won't see them. If you open a Terminal window and type ls to list files you still won't see them. However, you can find them by using ls -a in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can create one in your favorite text editor.

    Now, adding environment variables is relatively straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:

    export JAVA_HOME=/Library/Java/Home
    export JRE_HOME=/Library/Java/Home
    

    etc.

    If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:

    export PATH=$PATH:/path/to/my/stuff
    

    Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type .profile or .bash_profile to reload the file and have the contents be applied to your current Terminal's environment.

    You can check that your changes took effect using the "set" command in your Terminal. Just type set (or set | more if you prefer a paginated list) and be sure what you added to the file is there.

    As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.

    0 讨论(0)
  • 2020-11-22 11:31
    echo $PATH
    

    it prints current path value

    Then do vim ~/.bash_profile and write

    export PATH=$PATH:/new/path/to/be/added
    

    here you are appending to the old path, so preserves the old path and adds your new path to it

    then do

    source ~/.bash_profile
    

    this will execute it and add the path

    then again check with

    echo $PATH
    
    0 讨论(0)
提交回复
热议问题