Environment Variable Questions

Discussion in 'Mac Programming' started by ArtOfWarfare, Jan 12, 2016.

  1. ArtOfWarfare, Jan 12, 2016
    Last edited: Jan 12, 2016

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    I'm trying to make a utility which will help with managing your environment variables.

    One thing I'd like it to tell you is what all your environment variables are, and which line in which file set them to what they are.

    Right now, it checks the following files to determine this:

    By parsing out those files, it's able to tell you where each of these variables came from:
    LSCOLORS (line 6 of ~/.bash_profile, for me... I added it myself so it's not default)
    PATH (mix of ~/.bash_profile, /etc/paths, and /etc/paths.d/40-XQuartz)
    PS1 (line 4 of ~/.bash_profile)
    PS2 (line 5 of ~/.bash_profile)
    PYTHONSTARTUP (line 20 of ~/.bash_profile)

    So that's neat so far - especially the break down of PATH. But there's a lot of variables that my utility can't find the origin of yet. Does anyone happen to know where the following come from? I'd like to be able to present users with the line number and file where they can go to change the variable, even if that's a bad idea.

    I know some might actually be special variables not actually read from any file (IE, PWD and OLDPWD, perhaps) but surely some of these are variables that get read in from files that I just don't know about yet.

    _ (What is this one even? It's pointing at /Library/Frameworks/Python.framework/Versions/2.7/bin/python)

    Edit: Was hoping /etc/passwd would be useful and be where things like HOME, SHELL, and USER are set. It is not. It mentions something called opendirectoryd. I checked the man page for that, and it mentions a lot of files, but none of them seem to hold anything pertaining to the HOME, SHELL, or USER variables, either.
  2. mrichmon macrumors 6502a

    Jun 17, 2003
    A large part of your unknown list are being set by your shell "bash" directly. Remember that the environment variables are stored in the environment created by bash, and are interpreted by bash.

    Also, all of the files you've identified so far are loaded and interpreted by bash.

    So reading the bash man page will help you.

    For example, $PWD is set by the "cd" command that is built-in to bash. Similarly, $LANG, $SHLVL, $OLDPWD and $HOME are set and managed by bash.

    The $TERM* variables are set by login together with $USER and $LOGNAME. login also sets the initial $PATH before bash is executed typically resulting in $PATH being redefined as a side-effect of parsing and executing the various bash configuration scripts.

Share This Page