144

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?

55

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.

  • 1
    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
121

If zsh is your 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 the whole shell session output, or for the bash approach, use 7> file.log instead of 7>&2 to store the xtrace output to file.log instead of on the terminal).

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.

  • 4
    Impressive and helpful, thank you. For us n00bs, would you explain a bit more about the mode and how do we get out of the mode this puts us in? For example, we cannot simply pipe grep this output. – Geoffrey Hale Feb 10 '17 at 18:13
  • 5
    @GeoffreyHale if you zsh -xl 2>&1 , i.e. merge the stderr and stdout ouputs, you can grep as usual. – Rakesh Aug 7 '17 at 20:04
  • 1
    Is there any way to do this with the fish shell? – Nick Sweeting May 7 '18 at 16:42
46

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, /etc/bash.bashrc: meant for functions and aliases

User specific

  • ~/.bash_profile: initialization for login (bash-)shells
  • ~/.bashrc: initialization for all interactive (bash-)shells
  • ~/.profile: used for all shells
  • ~/.cshrc, ~/.zshrc, ~/.tcshrc: similar for non-bash shells
  • 1
    You've got the description for ~/.bash_profile and ~/.bashrc reversed. ~/.bash_profile is for login shells. ~/.bashrc is for interactive shells. – wisbucky Sep 22 '17 at 2:15
30

@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 ~/.*

  • 1
    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 '16 at 10:03
22

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_).

5

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

2

For zsh users, tracing the files that are accessed (during startup) can be useful, they are not too many and one can look through them one-by-one to find where something was defined.

zsh -o SOURCE_TRACE
0

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

  • 1
    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
-1

For example, if you want to find the HISTFILE variable and its value or want to know which variables is defined about history then you would type this in the shell:

set | grep HIST
env | grep HIST
printenv | grep HIST
  • if you already know the name, why not: echo "$HISTFILE" ? – Jeff Schaller Mar 29 '17 at 1:17
  • because, we want to find in where $HISTFILE variable is defined. Also if i wnat to know which variables is defined abou history – Vusal Aliyev Mar 29 '17 at 6:08
  • 1
    Unfortunately, set does not tell you which file defined the variable. – Jeff Schaller Mar 29 '17 at 9:52

protected by αғsнιη May 30 at 11:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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