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?

  • 14
    /etc/environment is another one.
    – derobert
    Commented Aug 20, 2010 at 18:44
  • 13
    And /etc/env.d/* files. But doing grep -R "YOUR_VARIABLE" /etc/ is probably the best way to find out. Commented Aug 28, 2011 at 0:53
  • 2
    On Mac OS X, see also How do I find where an environmental variable got set? Commented May 21, 2012 at 21:06
  • @rozcietrzewiacz the simplest way... if variable is indeed located somewhere under /etc/ (like in my case); if you post it as answer, I would upvote ;)
    – Line
    Commented Sep 2, 2019 at 9:34
  • In macos, /etc/env* do not exist. The answer in the macos comment also does not work. I will add macos as an additional tag to this question
    – R71
    Commented Jun 3, 2023 at 13:56

8 Answers 8


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.

  • 8
    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. Commented Feb 10, 2017 at 18:13
  • 16
    @GeoffreyHale if you zsh -xl 2>&1 , i.e. merge the stderr and stdout ouputs, you can grep as usual.
    – Rakesh
    Commented Aug 7, 2017 at 20:04
  • 2
    Is there any way to do this with the fish shell? Commented 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
    Commented Sep 8, 2019 at 14:42
  • 3
    Fantastic masterly possession of bash Commented Nov 11, 2019 at 19:58

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
  • 1
    This is comprehensive!
    – jdhao
    Commented 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
    Commented Nov 4, 2020 at 15:33
  • ~/.pam_environment was it, still set from back when I used Ubuntu, and now activated under Linux Mint Commented Feb 28, 2023 at 23:58

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
    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...) Commented 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
    Commented Nov 27, 2019 at 21:27

@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
    It's surprising that an answer that says 'you can't', surrounded by answers saying 'yes you can', has even this many upvotes. Commented 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. Commented Jul 31, 2015 at 14:13
  • This thing solved my time. Thanx. Commented 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
    Commented 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. Commented Jul 18, 2021 at 8:59

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


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.


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


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
    Commented Jan 7, 2015 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .