MacOS – What environment variables are set in .profile for OS X

bashmacos

I accidentally overwrote my .profile file and I believe deleted my environment variables. To be honest, I don't really understand how the operating system handles environment variables. My rough understanding is that there are configuration files like .profile which the operating system looks at, or runs, automatically and sets the environment variables that way. But how does the system know which config files to run? Are there default config files? Is .profile a default config file in OS X? If .profile is one of these default config files, is there a way for me to retrieve it or else the environment variables that it sets by default. What are the default values for .profile in OS X?

Best Answer

In your terminal that still has the variables write printenv > with_profile.txt. This will dump all available variables into with_profile.txt. Open a new terminal window and write printenv > without_profile.txt.

Start python and write:

with open("with_profile.txt") as w_profile, open("without_profile.txt") as wo_profile: 
    w_profile_vars = {line.strip() for line in w_profile}
    wo_profile_vars = {line.strip() for line in wo_profile}
    for var in w_profile_vars - wo_profile_vars: 
        print var

That should print the set of missing variables one by one.

P.S.

~/.profile is not there by default (not on Mavericks, Yosemite and El Capitan for sure). Hence it's possible that you overwrote nothing, unless you had created the file yourself a long ago.