command, which, and type.There are a few commands we can use for this. Notably, command -v is a built-in Bash command that does what we want, but there is also which, which is a separate program. The difference between these two is that command will also show you aliases and built-in commands, while which won't print anything when you have a built-in command and may give you the wrong information when you use an alias.
For example, on my Linux Mint, this is the output I get when I execute these commands [how?]: (below $ represents the shell prompt)
$ command -v which
/usr/bin/which
$ which which
/usr/bin/which
$ command -v command
command
$ which command
$ command -v ls
alias ls='ls --color=auto'
$ which ls
/usr/bin/ls
$ command -v pwd
pwd
$ which pwd
/usr/bin/pwd
Note: on my Linux Mint, /bin is symlinked to /usr/bin.
As we an see above, when the target command is a program, both commands give the same output (e.g. /usr/bin/which).
When the target command is a built-in, command outputs its name as-is while which outputs nothing. Observe how it happened with which command. We can use uppercase -V for a more verbose description.
$ command -V command
command is a shell builtin
When the command is an alias, command will give us the definition of the alias instead of its filepath. In this case, ls was aliased to ls plus the --color option, so we didn't get a filepath. which was able to give us the filepath in this case. However, there is a bit of a problem in this case:
The pwd command is both a shell built-in AND a program, and their behavior actually differs. When we use command -v pwd, we get the syntax that tells us pwd is a shell built-in, but when we use which, it simply shows us some pwd it found perhaps by searching the directories in the $PATH environment variable.
This means if this were the case with ls, for example, we would have no way of knowing that ls is a built-in command or not because it's an alias first.
How to Get the Type of an Alias?
Fortunately, there is a third method we can use to deal with aliases: type -a.
$ type -a pwd
pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
This command can show us multiple descriptions, in order. This means if we create an alias, we will still know which type it is because the built-in type will precede the program type. Observe:
$ alias pwd='pwd -P'
$ command -v pwd
alias pwd='pwd -P'
$ which pwd
/usr/bin/pwd
$ type -a pwd
pwd is aliased to `pwd -P'
pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd