3

Thanks to @ilkkachu I found a solution to this. I have posted it here and you can find it in the comments as well.

I want to unset one environment variable and I have searched all these places:

~/.bashrc
~/.profile
/etc/profile
/etc/environment
~/.bash_profile
/etc/bash.bashrc
/etc/profile.d/

As you can see, I have done my homework, but not only I can't find this specific environment variable, I am not able to find the majority of environment variables that env command shows either.

btw, I am using Debian 10.

PS:as this question has been asked numerous times on different forums, I guide anyone that has this question to use the function provided by @terdon in one of the answers. Unfortunately that doesn't solve my problem. But it definitely works in ordinary situations.

5
  • Try running bash -xl. This will run a new login shell and print all variables and functions that are set. Sep 27 at 15:49
  • @HenriMenke the problem is I can't find the file that this variables are set in
    – Masoud
    Sep 27 at 16:12
  • 1
    See: How to determine where an environment variable came from? (The accepted answer is useless, but Stéphane's xtrace trick should tell exactly what happens.)
    – ilkkachu
    Sep 27 at 18:13
  • 1
    If you provided the name of specific variable you want to unset, somebody might get an idea.
    – A.B
    Sep 27 at 18:40
  • @ilkkachu I don't know how to thank you. I used that bash command to search for it. It wasn't even there! But then I say a note at the end of that answer that stated that it might have happened during startup and I search xsession and boom. It was there!
    – Masoud
    Sep 27 at 18:57
4

I have a little bash function I use for this sort of thing:

grep_bash () 
{ 
    grep --color -H "$@" ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login ~/.bash_aliases /etc/bash.bashrc /etc/profile /etc/profile.d/* /etc/environment 2> /dev/null
}

That will cover most (all?) files that are commonly used in the various Linux flavors I am familiar with. It includes ~/.bash_aliases which is an Ubuntu thing, as far as I know. It also includes some standard ones you didn't search through, most importantly the files under /etc/profile.d.

If that doesn't find your variable, then you are most likely sourcing another file from inside one of those listed above. You can grep for all lines that are sourcing files and then go through those as well:

grep_bash -E '(\.|source) ' 

That will list all lines where one of the files listed above is sourcing another, so your variable might be defined in one of the sourced files. Of course, any one of these sourced files might be sourcing others in turn, so you might need to go deeper, but this should do as a starting point.

1
  • Thanks for your cool function. Unfortunately It couldn't find any of those env vars. I then used the extended version. It only contained a bunch of auto-completion files that I don't think are relevant.
    – Masoud
    Sep 27 at 16:28
1

Thanks to @ilkkachu I found a solution to this.

In the last part of the answer, it says that it might have happened during the startup (not so usual), and when I searched for my variable with this command from the answer: find /etc -type f -exec grep -F THE_VAR {} + I found it.

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