How can I find the file that a bash variable is defined there?

Assume the echo command shows

$ echo $DB_ROOT

Now, how can I find the file that this variable has been defined there? I have to say that I have not defined it in ~/.bashrc.


There are a large number of places where it could be defined.

For a full explanation, you need to look at the bash manpage, under INVOCATION, but I have to warn you that it will probably give you a headache before you get it all figured out :-)

Depending on how the shell is invoked and what files exist, it may be one or more of:

  • /etc/profile.
  • /etc/bash.bashrc
  • ~/.bash_profile.
  • ~/.bash_login.
  • ~/.profile.
  • ~/.bashrc.
  • whatever is specified with the --rcfile option.
  • whatever is specified with the BASH_ENV environment variable.
  • whatever is specified with the ENV environment variable.
  • any file called from the preceding files.

For what it's worth, I usually make sure I have a ~/.bash_profile (since that's the highest priority of the set {~/.bash_profile, ~/.bash_login, ~/.profile} - it will only process the first that it finds in that group).

I also make sure I have ~/.bashrc and that ~/.bash_profile sources it at the end.

That way, ~/.bash_profile holds only the stuff that I need in login shells and ~/.bashrc holds the stuff I want in every shell. You just have to make sure that the latter doesn't hold any "cumulative" commands (like adding something to the end of the path).

There's other ways to set it up, especially if you're using different shells and have to share profiles among them, but that scheme above is the easiest I've found in bash-only setups.

  • I found another difinition in ~/.profile. I removed it and sourceed again. However still the echo command shows the variable – mahmood Feb 8 '13 at 8:07
  • ok I found that I have to logoff/login because of `.profile'. Thanks man – mahmood Feb 8 '13 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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