263

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?

4

9 Answers 9

248

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 -Fw THE_VAR {} + may help.

13
  • 7
    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. Feb 10, 2017 at 18:13
  • 11
    @GeoffreyHale if you zsh -xl 2>&1 , i.e. merge the stderr and stdout ouputs, you can grep as usual.
    – Rakesh
    Aug 7, 2017 at 20:04
  • 2
    Is there any way to do this with the fish shell? May 7, 2018 at 16:42
  • 1
    I think you could just use PS4='+$BASH_SOURCE> ' BASH_XTRACEFD=7 bash -xl 7>/tmp/$(uuidgen), then grep on that random file as well.
    – bjd2385
    Sep 8, 2019 at 14:42
  • 3
    Fantastic masterly possession of bash Nov 11, 2019 at 19:58
109

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

  • ~/.profile: used for all shells
  • ~/.pam_environment: part of Pluggable Authentication Modules for Linux
  • ~/.bash_profile: initialization for login (bash-)shells
  • ~/.bashrc: initialization for all interactive (bash-)shells
  • ~/.cshrc, ~/.zshrc, ~/.tcshrc: similar for non-bash shells
3
  • This is comprehensive!
    – jdhao
    Oct 15, 2020 at 9:22
  • 6
    Also check ~/.pam_environment if on Ubuntu as this is the recommended location for storing user environment variables.
    – David
    Nov 4, 2020 at 15:33
  • ~/.pam_environment was it, still set from back when I used Ubuntu, and now activated under Linux Mint Feb 28 at 23:58
81

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.

2
  • 2
    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...) Jul 23, 2015 at 2:12
  • 4
    this doesn't answer his question. he doesn't want to know the value of the enviornmental variable, he wants to know the location of the configuration file the shell is referencing to get that environmental variable.
    – Andrew
    Nov 27, 2019 at 21:27
48

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

5
  • 5
    It's surprising that an answer that says 'you can't', surrounded by answers saying 'yes you can', has even this many upvotes. Jul 23, 2015 at 2:14
  • 1
    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. Jul 31, 2015 at 14:13
  • This thing solved my time. Thanx. Mar 1, 2016 at 10:03
  • @MichaelScheper "you can't" do it without find/grep. later in "you can" it's described how to do it USING grep
    – Line
    Sep 3, 2019 at 8:43
  • I tried changing it using sudo. It won't change for some reason. Even after I do echo varname, it still shows the old version. Jul 18, 2021 at 8:59
27

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

7

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

-1

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
  • 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, 2015 at 11:33
-3

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
3
  • if you already know the name, why not: echo "$HISTFILE" ?
    – Jeff Schaller
    Mar 29, 2017 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 Mar 29, 2017 at 6:08
  • 2
    Unfortunately, set does not tell you which file defined the variable.
    – Jeff Schaller
    Mar 29, 2017 at 9:52

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