38

I have command foo, how can I know if it's binary, a function or alias?

jcubic
  • 9,932
  • Related: http://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then – slm Sep 01 '13 at 13:28

3 Answers3

45

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

Note that you can have nested cases, such as an alias to a function. If so, to find the actual type, you need to unalias first:

unalias command; type command

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

fzbd
  • 148
Joseph R.
  • 39,549
7

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.

ericbn
  • 201
  • This is very helpful. I am new to zsh. Where can I find some documentation on this feature of zsh. I've skimmed through the documentation printed from the command run-help aliases. – tkolleh Feb 10 '20 at 20:40
  • 1
    @tkolleh, just added a link to the corresponding zsh documentation. – ericbn Feb 10 '20 at 22:28
5

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 
number5
  • 1,833
  • 1
  • 12
  • 12