1

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?

  • The list of available aliases isn't defined in any specific place; it's the result of all of the alias <name>=<definition> commands that have been executed in that shell process since it started. Shells automatically run the commands in the various init scripts at startup (which files are run depends on how the which shell program it is, how it's started, etc). Those can run other scripts (with the source or . commands). You can also define aliases manually. To find out where a particular alias is coming from, you have to trace the shell startup process. – Gordon Davisson Nov 1 '17 at 0:47
2

Along the lines of Stéphane's similar answer, 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'
  • For me, this command (bash -ix ..) only shows the aliases, no source. I guess this is because . can be used instead of source. – LoMaPh Nov 1 '17 at 1:45
  • Excellent point; I'll update the grep statement accordingly. – Jeff Schaller Nov 1 '17 at 1:46
  • I like PS4=' ${BASH_SOURCE}:$LINENO ' bash -lixc exit | grep ... for debugging. – muru Nov 1 '17 at 2:27
  • @JeffSchaller Thanks. I'm still having trouble finding the file. "the source command immediately above the alias of interest should be the file that contains the alias command." it seems this wasn't the case for me. I'll look more into it tomorrow and let u know. – LoMaPh Nov 1 '17 at 2:35
  • Ok. I found the problem. The alias was set through set-alias command (module environment). When I ran your suggested command, in the output the path /etc/profile.d/modules_environment.sh which loads user-defined modules as well appeared 4 lines above the alias definition. – LoMaPh Nov 1 '17 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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