Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a linux instance that I set up some time ago. When I fire it up and log in as root there are some environment variables that I set up but I can't remember or find where they came from. I've checked ~/.bash_profile, /etc/.bash_rc, and all the startup scripts. I've run find and grep to no avail. I feel like I must be forgetting to look in some place obvious. Is there a trick for figuring this out?

share|improve this question
2  
/etc/environment is another one. –  derobert Aug 20 '10 at 18:44
3  
And /etc/env.d/* files. But doing grep -R "YOUR_VARIABLE" /etc/ is probably the best way to find out. –  rozcietrzewiacz Aug 28 '11 at 0:53
    
On Mac OS X, see also How do I find where an environmental variable got set? –  Gilles May 21 '12 at 21:06

6 Answers 6

up vote 16 down vote accepted

If you use the env command to display the variables, they should show up roughly in the order in which they were created. You can use this as a guide to if they were set by the system very early in the boot, or by a later .profile or other configuration file. In my experience, the set and export commands will sort their variables by alphabetical order, so that listing isn't as useful.

share|improve this answer

If you put set -x in your .profile or .bash_profile, all subsequent shell commands will be logged to standard error and you can see if one of them sets these variables. You can put set -x at the top of /etc/profile to trace it as well. The output can be very verbose, so you might want to redirect it to a file with something like exec 2>/tmp/profile.log.

If your system uses PAM, look for pam_env load requests in /etc/pam.conf or /etc/pam.d/*. This module loads environment variables from the specified files, or from a system default if no file is specified (/etc/environment and /etc/security/pam_env.conf on Debian and Ubuntu). Another file with environment variable definitions on Linux is /etc/login.defs (look for lines beginning with ENV_).

share|improve this answer

@Cian is correct. Other than using find and grep, there isn't much you can do to discover where it came from. Knowing that it is indeed an environment variable, I would attempt focusing your search in /etc/ and your home directory. Replace VARIABLE with the appropriate variable you're searching for:

$ grep -r VARIABLE /etc/*

$ grep -r VARIABLE ~/.*

share|improve this answer

Check your startup scripts for files that they source using . (dot) or source. Those files could be in other directories besides /etc and $HOME.

share|improve this answer

If zsh is you login shell:

zsh -xl

With bash:

PS4='+$BASH_SOURCE> ' BASH_XTRACEFD=7 bash -xl 7>&2

That will simulate a login shell and show everything that is done (except in areas where stderr is redirected with zsh) along with the name of the file currently being interpreted.

So all you need to do is look for the name of your environment variable in that output. (you can use the script command to help you store that output).

If your variable is not in there, then probably the shell inherited it on startup, so it was set before like in PAM configuration, in ~/.ssh/environment, or things read upon your X11 session startup (~/.xinitrc, ~/.xsession) or set upon the service definition that started your login manager or even earlier in some boot script. Then a find /etc -type f -exec grep -F THE_VAR {} + may help.

share|improve this answer

Other than find and grep (which seem to have already failed), I'm afraid not.

share|improve this answer
    
This is really a comment, not an answer to the question. Please use "add comment" to leave feedback for the author. –  jasonwryan Aug 14 '12 at 1:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.