Set environment variables on Mac OS X Lion

2018-12-31 12:35发布

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 I have to do that, etc, and also why there seem to be so many different ones (why? Do they do different things?)

So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?

I need the variables to be set both for GUI applications and command line applications, and at the moment it's for an ant script that needs the variables, but there will most likely be other needs as well.

Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion...

Also note that I have practically zero experience using the Terminal. I'm willing to learn, but please explain for a novice...

15条回答
梦该遗忘
2楼-- · 2018-12-31 13:00

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.

查看更多
与君花间醉酒
3楼-- · 2018-12-31 13:01

First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:

~/.bash_profile

Update for Mavericks

OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.

Lion and Mountain Lion Only

OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:

~/.MacOSX/environment.plist

If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.

Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).

It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.

One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.

Edit: Here are some instructions for creating a plist file.

  1. Open Xcode
  2. Select File -> New -> New File...
  3. Under Mac OS X select Resources
  4. Choose a plist file
  5. Follow the rest of the prompts

To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.

Once the plist file is created you can open it with Xcode to modify it anytime you wish.

查看更多
还给你的自由
4楼-- · 2018-12-31 13:02

Open Terminal:

vi ~/.bash_profile

Apply changing to system (no need restart computer):

source ~/.bash_profile

(Also work with macOS Sierra 10.12.1)

查看更多
浅入江南
5楼-- · 2018-12-31 13:07
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
查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 13:09

I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:

  1. Go to /Applications/eclipse/Eclipse.app/Contents folder

  2. Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:

    <dict>
      <key>LSEnvironment</key>
      <dict>
            <key>PATH</key>
            <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string>
      </dict>
      <key>CFBundleDisplayName</key>
      <string>Eclipse</string>
      ...
    
  3. Reload parameters for app with

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
    
  4. Restart Eclipse

查看更多
冷夜・残月
7楼-- · 2018-12-31 13:09

I took the idiot route. Added these to the end of /etc/profile

for environment in `find /etc/environments.d -type f`
do
     . $environment
done

created a folder /etc/environments create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.

/etc$ cat /etc/environments.d/Oracle

export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
查看更多
登录 后发表回答