Bash – How to Check Where an Alias Was Defined

bashbashrcterminal

An alias, such as ll is defined with the alias command.

I can check the command with things like type ll which prints

ll is aliased to `ls -l --color=auto'

or command -v ll which prints

alias ll='ls -l --color=auto'

or alias ll which also prints

alias ll='ls -l --color=auto'

but I can't seem to find where the alias was defined, i.e. a file such as .bashrc, or perhaps manually in the running shell. At this point I'm unsure if this is even possible.

Should I simply go through all files that are loaded by bash and check every one of them?

Best Answer

Manual definition will be hard to spot (the history logs, maybe) though asking the shell to show what it is doing and then grep should help find those set in a rc file:

bash -ixlc : 2>&1 | grep ...
zsh -ixc : 2>&1 | grep ...

If the shell isn't precisely capturing the necessary options with one of the above invocations (that interactively run the null command), then script:

script somethingtogrep thatstrangeshell -x
...
grep ... somethingtogrep

Another option would be to use something like strace or sysdig to find all the files the shell touches, then go grep those manually (handy if the shell or program does not have an -x flag); the standard RC files are not sufficient for a manual filename check if something like oh-my-zsh or site-specific configurations are pulling in code from who knows where (or also there may be environment variables, as sorontar points out in their answer).

Related Question