Is it possible to check where an alias was defined?

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).


Here is where I find grep -rl very useful:

grep -rl alias ~/.bash* ~/.profile /etc/profile /etc/bash.bashrc

will tell you in which file the word alias is used.

Probably in ~/.bashrc and most certainly in ~/.bash_aliases if it exists.


It is however impossible to be absolutely sure that that covers all options. Those files may also call or load any other files. An environment variable like ENV or $BASH_ENV may direct bash to load some other files.

looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.

And aliases may even be defined by setting a variable (emphasis mine):

BASH_ALIASES
An associative array variable whose members correspond to the internal list of aliases as maintained by the alias builtin. Elements added to this array appear in the alias list


First use the following commands

List all functions

functions 

List all aliases

alias 

If you aren't finding the alias or function consider a more aggressive searching method

Bash version

bash -ixlc : 2>&1 | grep thingToSearchHere

Zsh version

zsh -ixc : 2>&1 | grep thingToSearchHere

Brief Explanation of Options

-i     Force shell to be interactive.

-c     Take the first argument as a command to execute

-x      -- equivalent to --xtrace

-l      Make bash act as if invoked as a login shell