• Howto set shell-environment variables on Mac OSX 10.5

    Posted on April 12th, 2009 Nattl 6 comments

    From time to time I want to install a command line utility and then I always have the problem how to set the correct environment variables. Ok, googling for the terms “mac osx environment variable set” usually helps. But every time I spent at least an hour playing around, searching like made before I can use my command-line stuff. The funny thing is that this has happened several times in the past and each time I somehow forgot how to define the environment variables ( that’s so me…). But this time things will be different as I write it down in my own blog. So the next time I can google myself and find out about how to do it ;).

    Basically there are several ways to achieve this goal. I’ll show all of which I know (there might be even more I have no idea about).

    The simplest thing is to simply use the export command:

    $>export VAR_NAME=ValueToBeSet

    This works fine but the drawback is that you have to set the variable each time you start the shell.

    Another way is to create a hidden directory .MacOSX in your home directory and then create an environment.plist in it.

    Open a terminal and type

    $>cd

    just to make sure you are in your home-directory. Then create the folder .MacOSX (the . in front of the filename makes it invisible so it won’t be seen in the finder)

    $>mkdir .MacOSX

    Now lets create the environment.plist

    $>touch .MacOSX/environment.plist

    and open it

    $>open .MacOSX/environment.plist

    the file will now be opened in the Property List editor.

    plist

    Now you can simply add the new environment variable by clicking on the Add Item-button. It should consist of a key/value pair. After editing the  environment.plist you save it. Now all you have to do is to log out and in again to bring the new environment variables into action.

    The above works as long as you don’t have to alter the $PATH variable. It stores all the paths to executables in your system, so simply overwriting it would be a bad idea. So usually you append the path you want to add

    $>export PATH=$PATH:/New/Path/to/add

    Of course you could type this in your terminal, it would work but it would be not persistent.

    Well, the easist way to alter your path variable is to change it each time you start the terminal, wouldn’t it? For that reason there is a configuration file for the terminal in your home-folder. Its a hidden file called .bash_profile - however there is a chance that it doesn’t exist on your system, but you could easily create it. To check if it exists type

    $>ls -l bash*

    if the result doesn’t show a file called .bash_profile you can simply create it either with the touch-command. But its even easier to do it with a text-editor of your choice (TextEdit, Smultron, Textmate, etc.). As .bash_profile is a simple textfile that can hold commands that will be executed on terminal-startup you can simply add all the stuff you want to this file and then save it to your home-directory (that one called with your username). Saving it with a . in front of the name will make Mac OSX show a message that using a dot is reserved for the system, but you can save it anyway. As I mentioned before you can write any command into the .bash_profile and it will be executed on terminal startup. So you can easily define all the variables with the export command and then add it to the PATH environment variable:

    export APP_HOME=/opt/app
    export FOO_HOME=/usr/bin/foo
    export PATH=$PATH:$APP_HOME:$FOO_HOME

    When altering $PATH don’t forget that the different paths have to be separated by a colon ( : ). After saving and restarting the terminal the new paths should already work.

    $>echo $PATH

    /bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin:/usr/texbin:/opt/app:/usr/bin/foo

    So this way is IMHO the simplest way to define environment variables on the Shell. But there is another way…

    Hidden deep in the Unix-internals of Mac OSX you could also define PATH-variables… in the /etc-directory there are a file called paths and a directory called paths.d. While paths stores the standard paths it is possible to add additional paths as files in the paths.d-directory. So lets say you have a new command line binary stored at /opt/someapp you simply add a new file called someapp to the paths.d directory. Stored within this file is the path /opt/someapp.  You can invoke it  with the path_helper command which you could start from your .bash_profile as shown above.

    Well I hope this little howto was of any use for you.

     

    5 responses to “Howto set shell-environment variables on Mac OSX 10.5” RSS icon

    • The simplest way to set path!! :)

    • This does not seem to work. I added 3 environment variables, logged out and back in, also restarted computer and it only recognized the first 2 environment variables. Here’s the environment.plist file.

      ACE_LIBDIR
      /Users/rbottone/ACE_wrappers/build/ace/.libs
      ACE_ROOT
      /Users/rbottone/ACE_wrappers
      DYLD_FALLBACK_LIBRARY_PATH
      /Users/rbottone/ACE_wrappers/build/ace/.libs
      DYLD_LIBRARY_PATH
      /Users/rbottone/ACE_wrappers/build/ace/.libs
      LD_LIBRARY_PATH
      /Users/rbottone/ACE_wrappers/build/ace/.libs

      I have Mac OSX 10.6.2 leopard.

      env command from terminal shows the following:

      TERM_PROGRAM=Apple_Terminal
      TERM=xterm-color
      SHELL=/bin/bash
      TMPDIR=/var/folders/va/vav1IwH2FYi0NfkaKxEZmU+++TI/-Tmp-/
      Apple_PubSub_Socket_Render=/tmp/launch-UjZNyQ/Render
      TERM_PROGRAM_VERSION=272
      USER=rbottone
      COMMAND_MODE=unix2003
      SSH_AUTH_SOCK=/tmp/launch-h2vuKT/Listeners
      __CF_USER_TEXT_ENCODING=0×1F5:0:0
      ACE_ROOT=/Users/rbottone/ACE_wrappers
      PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
      PWD=/Users/rbottone/.MacOSX
      LANG=en_US.UTF-8
      ACE_LIBDIR=/Users/rbottone/ACE_wrappers/build/ace/.libs
      SHLVL=1
      HOME=/Users/rbottone
      LOGNAME=rbottone
      DISPLAY=/tmp/launch-XV6OCT/:0
      _=/usr/bin/env
      OLDPWD=/Users/rbottone

      Any ideas?

    • I also see this behaviour.
      In particular, it seems as if all variables that start with DYLD are ignored!

      Weird…

    • Sorry, I was not precise in my previous post:
      I can set all variables in environment.plist, but when I try to get them into the shell via these lines in my bash_profile…

      for i in `defaults read $HOME/.MacOSX/environment \
      | sed -nE ’s/^ ([a-zA-Z0-9]+) = (.+”);/\1/p’`; do
      eval export $i=”`defaults read $HOME/.MacOSX/environment $i`”
      done

      … it seems that all variables are exported except those whose name starts with DYLD.

    • Since I upgraded to 10.6 I stick with the quick and dirty way: using export. In your home-directory you have a file called .bash_profile - either open it with an editor (using the command open -a youreditorofchoice.app .bash_profile) or if you are hardcore with vi. Enter the environment-variable you want to add:

      export GRAILS_HOME=/Users/nat/development/grails

      If you want to add a path to $PATH you add

      export PATH=$PATH:

      Save the file and restart the terminal. That’s it.


    1 Trackbacks / Pingbacks

    Leave a reply