如何测试命令是别名、函数还是二进制?

jcu*_*bic 27 shell alias function

我有 command foo,我怎么知道它是二进制的,函数还是别名?

Jos*_* R. 30

如果您使用 Bash(或其他类似 Bourne 的 shell),则可以使用type.

type command
Run Code Online (Sandbox Code Playgroud)

将告诉您command是 shell 内置、别名(如果是,别名是什么)、函数(如果是,它将列出函数体)或存储在文件中(如果是,则是文件的路径) .

请注意,您可以有嵌套的 case,例如函数的别名。如果是这样,要找到实际类型,您需要先取消别名:

unalias command; type command
Run Code Online (Sandbox Code Playgroud)

有关“二进制”文件的更多信息,您可以执行

file "$(type -P command)" 2>/dev/null
Run Code Online (Sandbox Code Playgroud)

如果command是别名、函数或 shell 内置,则不会返回任何内容,但如果它是脚本或编译的二进制文件,则返回更多信息。

参考


eri*_*cbn 7

在 zsh 中,您可以检查aliasesfunctionscommands数组。

(( ${+aliases[foo]} )) && print 'foo is an alias'
(( ${+functions[foo]} )) && print 'foo is a function'
(( ${+commands[foo]} )) && print 'foo is an external command'
Run Code Online (Sandbox Code Playgroud)

还有builtins, 用于内置命令。

(( ${+builtins[foo]} )) && print 'foo is a builtin command'
Run Code Online (Sandbox Code Playgroud)

编辑:检查zsh/parameter 模块文档以获取可用数组的完整列表。


num*_*er5 5

答案取决于您使用的 shell。

对于 zsh,shell 内置命令whence -w会准确地告诉你你想要什么

例如

$ whence -w whence
whence : builtin
$ whence -w man     
man : command 
Run Code Online (Sandbox Code Playgroud)