How can I find a rogue alias declaration?

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

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.


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?

Tags:

Bash

Alias

Bashrc