Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to find out which files have defined "alias"es?

When I run the alias command, I see an alias which I don't know where it has been defined (i.e. I cannot find the file which contains definition of this alias). I have checked /etc/profile, ~/.bashrc, ~/.profile, ~/.bash_profile, ~/.bash_login, /etc/bash.bashrc and some others but to no avail.

Is there a way to find out which files the alias command gets its info from? I mean how does the alias command work? Does it read some predetermined files looking for alias definitions?
by

1 Answer

Kajalsi45d
here's a way to run an interactive bash shell in trace mode (running the single command exit), piped to a grep that shows only the source and alias commands; the source command immediately above the alias of interest should be the file that contains the alias command.
bash -ix -c exit 2>&1 | grep -E 'source | \. |alias '

An instrumented run:
$ tail -1 ~/.bashrc
[ -f /tmp/a.bashrc ] && source /tmp/a.bashrc
$ cat /tmp/a.bashrc
[ -f /tmp/b.bashrc ] && . /tmp/b.bashrc
$ cat /tmp/b.bashrc
alias answer='echo 42'

$ bash -ix -c exit 2>&1 | grep -E 'source | \. |alias '
+ alias 'ls=ls --color=auto'
+ source /tmp/a.bashrc
++ . /tmp/b.bashrc
+++ alias 'answer=echo 42'

Login / Signup to Answer the Question.