38

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

1

3 Answers 3

44

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

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

2
  • 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, 2020 at 20:40
  • 1
    @tkolleh, just added a link to the corresponding zsh documentation.
    – ericbn
    Feb 10, 2020 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 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .