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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
There’s a few things you can try:
- use
bash -vto see what lines are being read during shell startup - use
bash -xto see what commands are being run during shell startup - 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.
Method 3
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?
Method 4
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0