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
add comment

5 Answers 5

up vote 12 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
add comment

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
add comment

@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
add comment

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
add comment

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
add comment

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.