How to test if command is alias, function or binary?
If you're on Bash (or another Bourne-like shell), you can use type
.
type command
will tell you whether command
is a shell built-in, alias (and if so, aliased to what), function (and if so it will list the function body) or stored in a file (and if so, the path to the file).
For more information on a "binary" file, you can do
file "$(type -P command)" 2>/dev/null
This will return nothing if command
is an alias, function or shell built-in but returns more information if it's a script or a compiled binary.
References
- Why not use "which"? What to use then?
The answer will depends on which shell you're using.
For zsh, shell builtin whence -w
will tell you exactly what you want
e.g.
$ whence -w whence
whence : builtin
$ whence -w man
man : command
In zsh you can check the aliases
, functions
, and commands
arrays.
(( ${+aliases[foo]} )) && print 'foo is an alias'
(( ${+functions[foo]} )) && print 'foo is a function'
(( ${+commands[foo]} )) && print 'foo is an external command'
There's also builtins
, for builtins commands.
(( ${+builtins[foo]} )) && print 'foo is a builtin command'
EDIT: Check the zsh/parameter module documentation for the complete list of arrays available.