Where does OS X store environment variables?

I'm new to OS X but I've used Linux (and a Unix a while ago) on and off. I'm a bit confused as to how the particular version of Unix Mac OS X uses works.

In Linux, I kept environment variables in .bash and .bash_profile files in my home directory. What is the default file Mac OSX uses to store all this information? So, for instance if I do

$env

I get information about my environment such as:

PATH=/usr/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/ usr/X11/bin
...
USER=josepmfontana
...
HOME=/Volumes/DATA

The problem comes when I try to modify this information. I even did a grep:

Josep-M-Fontanas-iMac:~ josepmfontana$ grep -r USER=josepmfontana ~/.*

But I only found this string in .bash_history

Actually, I found another unexpected problem. grep stalled with the following error message:

grep: warning: /Volumes/DATA/../Macintosh HD/Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/Res ources: recursive directory loop

I've checked a few manuals, I've Googled for default environment variables Mac OS X but I'm stumped.

Any help I can get from the community will be greatly appreciated.

Josep M.

iMac 27" Core i7, Mac OS X (10.6.4)

Posted on Jun 25, 2010 4:30 AM

Reply
26 replies

Jun 25, 2010 5:07 AM in response to jfontana

Basically, OS X and Linux are mostly the same.

First, it's not the operating system that decides where the environment variables go, it's the shell. For example, the 'bash' shell that's the default on Linux and OS X systems. If you read the manual page for bash, you will see the following nugget:

{quote}

When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.

When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands
its value if it appears there, and uses the expanded value as the name
of a file to read and execute. Bash behaves as if the following com-
mand were executed:
if \[ -n "$BASH_ENV" \]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file
name.
{quote}

Different shells (like tcsh, for example) follow different rules.

One big difference is that most Linux environments have a directory, /etc/profile.d, that /etc/profile will look to for individual environment setup scripts. OS X does not.

Jun 25, 2010 12:22 PM in response to J D McIninch

Thanks for your fast answers Tom and J.D. I take note of Tom's recommendation and I will post questions concerning Unix on the other forum in the future.

Well, OS X and Linux might be very similar but I find that some things are a bit different.

First, there was no .bash_profile file in my home directory when I started working with my computer or, if there was, it must have been empty because all its contents were added by the installation of Python version 2.6.5 I downloaded from python.org.

Inspecting things a bit more I found other path variables in ~/.profile. Then googling around I found some forums where some people claimed that the default file for environment variables was ~/.MacOSX/environment.plist (I don't have any directory named /.MacOSX or any file named environment.plist). Searching some more I also found out that additional places where path variables are stored are /etc/paths and /etc/paths.d/

Josep M.

Jun 25, 2010 1:15 PM in response to jfontana

jfontana wrote:
First, there was no .bash_profile file in my home directory when I started working with my computer or, if there was, it must have been empty because all its contents were added by the installation of Python version 2.6.5 I downloaded from python.org.


One of the differences between Linux and MacOS X is that you shouldn't always be quick to download the latest and greatest of everything. There is no guarantee it will work properly on MacOS X. The software that Apple provides (such as Python version 2.6.1) is custom built for MacOS X by people who know it better than anyone - Apple.

Inspecting things a bit more I found other path variables in ~/.profile. Then googling around I found some forums where some people claimed that the default file for environment variables was ~/.MacOSX/environment.plist (I don't have any directory named /.MacOSX or any file named environment.plist). Searching some more I also found out that additional places where path variables are stored are /etc/paths and /etc/paths.d/


All of the above are true. Variables in /etc/paths.d (known as path_helper) will apply to all users of the machine all the time. Variables in ~/.MacOSX/environment.plist will apply to a single user's entire session. Variables in the various shell init files follow established, confusing rules about when they apply.

Jun 25, 2010 2:41 PM in response to etresoft

Thanks again.

{quote:title=etresoft wrote:}
All of the above are true. Variables in /etc/paths.d (known as path_helper) will apply to all users of the machine all the time. Variables in ~/.MacOSX/environment.plist will apply to a single user's entire session. Variables in the various shell init files follow established, confusing rules about when they apply.{quote}

OK, as I said, I don't have a directory called .MacOSX or a file called environment.plist. Should I create them to have all my environment variables centralized in one place? Or should I do that in .bash_profile?

Josep M.

Jun 25, 2010 3:10 PM in response to jfontana

I store all of mine in a .bash_profile file located in my home directory. I don't use system-wide profiles.

My paths are set as follows (Xcode Tools installed):

export PATH=$PATH:/usr/local/bin:/Developer/Tools:/Developer/usr/bin:/usr/X11/bin
export MANPATH="/usr/share/man:/usr/local/share/man:/Developer/usr/share/man:/usr/X11R 6/man"

Jun 25, 2010 3:44 PM in response to jfontana

System Wide info is stored in the /private/etc/bashrc and /private/etc/profile files.

I do not recommend jacking with those unless you know what you are doing. You can
easily accomplish needed mods in your ~/.profile file. If you create a ~/.bash_profile file instead,
just remember ~/.profile will no longer be read, which may effect some third party software that
write environment entries in the ~/.profile file. An example of a program that might use the
~/.profile file is MacPorts.
example:
#Setting the path for MacPorts.
#export PATH=/opt/local/bin:/opt/local/sbin:$PATH

I removed MacPorts so I commented out the MacPorts export line in my ~/.profile file.

Jun 25, 2010 4:07 PM in response to jfontana

jfontana wrote:
OK, as I said, I don't have a directory called .MacOSX or a file called environment.plist. Should I create them to have all my environment variables centralized in one place? Or should I do that in .bash_profile?


There are a few such "special" directories that don't exist by default, but could be created and used if desired.

There really isn't a good answer for that. I would recommend against .MacOSX/environment.plist just because it is tucked away in such a hidden place. But if you need an environment variable to be available in the Finder (due to some flaky open-source tools ported to MacOS X), then you may need to use the environment.plist or path_helper. Otherwise, .bash_profile is fine and is the expected place one would look for such things.

Jun 25, 2010 5:33 PM in response to jfontana

jfontana wrote:
Inspecting things a bit more I found other path variables in ~/.profile. Then googling around I found some forums where some people claimed that the default file for environment variables was ~/.MacOSX/environment.plist (I don't have any directory named /.MacOSX or any file named environment.plist). Searching some more I also found out that additional places where path variables are stored are /etc/paths and /etc/paths.d/


This is correct to my level of knowledge. I don't use unix, but I use latex. Environment variables for latex (such as BIBINPUTS) are stored in ~/.MacOSX/environment.plist. If you don't have the directory or the file you can create it. I don't know anything about environment variables in general, have only used it for latex.

Jun 26, 2010 4:08 AM in response to KJK555

Thank you to everyone that responded. The links you guys have supplied have been very helpful to clarify things. I must say this is a very nice and helpful community.

My conclusion is that if I want to have all the environment information centralized in one place my best bet is ~/.profile since this is where most third party apps are going to store information.

JM

Jun 26, 2010 10:33 AM in response to jfontana

Hi jfontana:

Thanks for the feedback.

I suppose that if you really want to name it ".bash_profile" you could, then create a symbolic
link to it named ".profile", that way third party software would still find it.
note: do not use an alias (from finder), it will not work properly.

Here is a free symbolic link program I have used for years:
http://seiryu.home.comcast.net/~seiryu/symboliclinker.html

Kj ♘

Message was edited by: KJK555

Jun 26, 2010 1:46 PM in response to jfontana

jfontana wrote:
My conclusion is that if I want to have all the environment information centralized in one place my best bet is ~/.profile since this is where most third party apps are going to store information.


Actually, the file you want to use is .bash_profile. No good 3rd party app would ever mess with your .profile file. I've been using MacOS X since the beta days and I've never had a .profile file.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Where does OS X store environment variables?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.