34

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
jcubic
  • 9,612
  • 16
  • 54
  • 75
  • Related: http://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then – slm Sep 01 '13 at 13:28

3 Answers3

40

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
  • 4
Joseph R.
  • 38,849
  • 7
  • 107
  • 143
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
  • 2
  • 3
  • 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,813
  • 1
  • 12
  • 12