I have a bash script that needs to behave differently if a particular alias is defined. Is there a way to test if a particular command is an alias in bash?
I’m gonna make @jimmij’s comment from 5 years ago into an answer:
type -t is designed for exactly that purpose. It will output
alias if the given command is an alias. If the command does not exist, it won’t output anything, which means you don’t need to redirect its stderr or something.
if [ "$(type -t foo)" = 'alias' ]; then echo 'foo is an alias' else echo 'foo is not an alias' fi
Note that the
-t switch is not defined in POSIX. Shells other than bash might not implement it.