Benches macOS

17

Where does OS X store environment variabl…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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.

17 replies

Sign in or join Beachball Help to reply.

  1. 0

    Tom Gewecke

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The best place to ask about command line issues is the Unix forum:

    http://discussions.apple.com/forum.jspa?forumID=735

  2. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    jfontana wrote:

    What is the default file Mac OSX uses to store all this information?

    Perhaps it would be good to step back a minute and ask some basic questions. It is common for people to start down a path, get stuck and then ask for help on how to resolve the sticky situation. The correct answer is often that they are on the wrong path to begin with.

    What problem are you experiencing that causes you to ask this question?

    PATH=/usr/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/ usr/X11/bin

    There are certainly some redundancies in there. It should look like:

    PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bi n

    HOME=/Volumes/DATA

    I don't like the looks of this. How did you accomplish this? It doesn't look right. If you want your home directory to be on a different volume, it would be best to mount that volume at /Users.

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

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

    You can't modify USER or HOME this way. Well, perhaps you can, but you shouldn't. Bad things will happen. If you want to change USER, you should create a new user account. If you want to change HOME, you should do it through the Accounts pane in System Preferences. Select your account and right/control click on it to bring up "Advanced Options"

    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

    That just looks all screwed up.

  3. 0

    Tom Gewecke

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The best place to ask about command line issues is the Unix forum:

    http://discussions.apple.com/forum.jspa?forumID=735

  4. 0

    J D McIninch

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  5. 0

    jfontana

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  6. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  7. 0

    jfontana

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  8. 0

    KJK555

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    by default most third party programs write environment values in the ~/.profile file in your home

    directory. If it does not exist, it will be created.

    Kj

  9. 0

    baltwo

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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"

  10. 0

    KJK555

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  11. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  12. 0

    KJK555

    Imported from the Apple Support Forums via the Wayback Machine 🔗

  13. 0

    ahostmadsen

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  14. 0

    KJK555

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Most users probably shouldn't concern themselves with this old open step relic,

    but for the curious here are some links about the ~/MacOSX/environment.plist:

    http://lists.apple.com/archives/X11-users//2007/Mar/msg00060.html

    http://lists.apple.com/archives/x11-users/2004/Dec/msg00085.html

    http://www.rubicode.com/Software/RCEnvironment/

    Message was edited by: KJK555

  15. 0

    jfontana

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  16. 0

    KJK555

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  17. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.