Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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.

share|improve this question

migrated from Feb 8 '13 at 8:43

This question came from our site for professional and enthusiast programmers.

up vote 7 down vote accepted

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.

share|improve this answer
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 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.