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?

This question came from our site for professional and enthusiast programmers.

.profile is not a Bash file, exclusively. It is read by sh and possibly other shells as well.
– tripleeeApr 7 '16 at 5:29

Most of environment variables are not set by .profile. By default, that file does not even exist. Whatever problem you have, it is not because you have overwritten your .profile.
– hamstergeneApr 9 '16 at 20:33

1 Answer
1

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.