Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
/etc/environment is another one. – derobert Aug 20 '10 at 18:44
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
up vote 26 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
That's great... except I'm trying to find out what's clearing an environment variable (set in /etc/environment). :-) (And yes, it's being set initially... I'm adding lines in scripts in various to log where it gets cleared...) – Michael Scheper Jul 23 '15 at 2:12

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

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

@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
It's surprising that an answer that says 'you can't', surrounded by answers saying 'yes you can', has even this many upvotes. – Michael Scheper Jul 23 '15 at 2:14
I don't see any answers that actually give a way to determine WHERE a variable was set. There are some useful clues, but no one-liner that can do the job. – Andrew Wagner Jul 31 '15 at 14:13
This thing solved my time. Thanx. – Hassan Raza Mar 1 at 10:03

Some places to look first:

System wide

  • /etc/environment: specifically meant for environment variables
  • /etc/env.d/*: environment variables, split in multiple files
  • /etc/profile: all types of initialization scripts
  • /etc/profile.d/*: initialization scripts
  • /etc/bashrc: meant for functions and aliases

User specific

  • ~/.bash_profile: initialization for all interactive (bash-)shells
  • ~/.bashrc: initialization for login (bash-)shells
  • ~/.profile: used for all shells
  • ~/.cshrc, ~/.tcshrc, ~/.zshrc, ~/.tcshrc: similar for non-bash shells

Disclaimer: I'm not an expert, I just listed some places I collected.

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

environment variables are stored in /etc/profile file so do more /etc/profile and just check for env variables you want and if /etc/profile is not present then lokk for .profile file in your home directory

share|improve this answer
Environment variables are not stored in /etc/profile, you can define them there system wide for e.g. bash when used as a login shell. They are stored by the shell process after reading in definitions from files and/or commandline. – Anthon Jan 7 '15 at 11:33

Your Answer


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.