40

I'm trying to find where a specific alias has been declared. I've searched all the usual places I know to look for aliases:

  • ~/.bashrc
  • ~/.bash_profile
  • /etc/bashrc
  • /etc/profile

With no luck.

I know it's an alias because when I do which COMMAND, I get:

alias COMMAND='/path/to/command'
    /path/to/command

Is there a way to find what file declares an alias only knowing the alias name?

utopiabound
  • 3,304
  • 3
    A related question about environment variables instead of aliases. Basically, there is no easy way since the alias could have been set anywhere. Try putting set -x as suggested at the top of your and see if that narrows it down for you. – jw013 May 10 '12 at 12:37
  • 1
    You'd have to put set -x at the top of /etc/bash_profile or /etc/bashrc. Why not just run bash -i -x or bash -l -x? – Mikel May 10 '12 at 23:36

4 Answers4

31

I would look in /etc/profile.d/ for the offending alias.

You could also do the following to find it:

grep -r '^alias COMMAND' /etc

This will recursively grep through files looking for a line beginning with alias COMMAND.

If all else fails, put this at the end of your ~/.bashrc

unalias COMMAND
utopiabound
  • 3,304
30

There's a few things you can try:

  1. use bash -v to see what lines are being read during shell startup
  2. use bash -x to see what commands are being run during shell startup
  3. run with only one startup file

bash -v

The -v option makes bash print each line from every script file it reads as it reads it.

Start by running

bash -i -v >bash-i.out 2>&1

wait 5-10 seconds, then press Ctrl+C.

This will give you a single file called bash-i.out that is like all your startup files merged (or concatenated) together.

Then use less to open the file and search for the alias using /aliasname.

Now, compare where that alias appears in relation to other lines in the file. For example, on most systems, /etc/bash.bashrc has a comment at the top that says /etc/bash.bashrc and ~/.bashrc has one too.

If it's above the top of your ~/.bashrc, then it's probably a startup file in /etc that's defining the alias, otherwise it's in your ~/.bashrc or a file it's including via source or . (dot command).

If that doesn't show the alias, try

bash -l -v >bash-l.out 2>&1

That tells bash to be a login shell, which reads some different startup files, for example /etc/profile and ~/.bash_profile instead of /etc/bash.bashrc and ~/.bashrc.

bash -x

If bash -v doesn't give you a definite answer, try running bash -x, which prints the commands the shell is running, rather than the lines your shell is reading.

The method is basically the same as the above except change -v to -x. (You can use both together if necessary.)

Run with only one startup file

bash -i --rcfile="$HOME/.bashrc"

and see if you have the alias.

Try the same with rcfile set to /etc/bash.bashrc if your system has it.

Then try

bash -l --rcfile="$HOME/.bash_profile"

and do the same with every bash startup file that has profile in its name, e.g. change $HOME/.bash_profile to /etc/profile.

Whichever way makes the alias appear tells you the file you should start looking at.

Mikel
  • 57,299
  • 15
  • 134
  • 153
2

Maybe your .xinitrc? You should also check to see if your .bashrc or .bash_profile source any other files in them. For example, I keep all my aliases in a separate file referenced by this command:

[ -f ~/.bash_alias ] && source $HOME/.bash_alias

Some questions which might also help: Is this for a regular or root user? Which command is it? What flavour of Linux?

0

I follow the very common practice of putting my alias definitions into

~/.bash_aliases

and then calling that from .bashrc with

[ -f ~/.bash_aliases ] && source $HOME/.bash_aliases
Minzkraut
  • 143
  • 1
    can i guess that you mean .bash_aliases in your second command instead of .bash_alias? – stagl Jun 07 '17 at 02:53