如何显示 zsh 函数定义(如 bash “type myfunc”)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11478673/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show zsh function definition (like bash "type myfunc")?
提问by Rob Bednark
How do I show the definition of a function in zsh? type foodoesn't give the definition.
如何在 zsh 中显示函数的定义?type foo没有给出定义。
In bash:
在 bash 中:
bash$ function foo() { echo hello; }
bash$ foo
hello
bash$ type foo
foo is a function
foo () 
{ 
    echo hello
}
In zsh:
在 zsh 中:
zsh$ function foo() { echo hello; }
zsh$ foo
hello
zsh$ type foo
foo is a shell function
回答by pb2q
The zsh idiom is whence, the -fflag prints function definitions:
zsh 惯用语是whence,-f标志打印函数定义:
zsh$ whence -f foo
foo () {
    echo hello
}
zsh$
In zsh, typeis defined as equivalent to whence -v, so you can continue to use type, but you'll need to use the -fargument:
在 zsh 中,type定义为等效于whence -v,因此您可以继续使用type,但您需要使用-f参数:
zsh$ type -f foo
foo () {
    echo hello
}
zsh$
And, finally, in zsh whichis defined as equivalent to whence -c- print results in csh-likeformat, so which foowill yield the same results.
最后,在 zshwhich中定义为等效于whence -c- 以类似 csh 的格式打印结果,因此which foo将产生相同的结果。
man zshbuiltinsfor all of this.
man zshbuiltins对于这一切。
回答by Thor
I've always just used whichfor this.
我一直只是用which这个。
回答by mklement0
tl;dr
tl;博士
declare -f foo  # works in zsh and bash
typeset -f foo  # works in zsh, bash, and ksh
type -f/ whence -f/ whichare suboptimal in this case, because their purpose is to report the command form with the highest precedencethat happens to be definedby that name(unless you also specify -a, in which case allcommand forms are reported) - as opposed to specifically reporting on the operand as a function. 
type -f/ whence -f/which是在这种情况下不理想的,因为他们的目的是报告用命令的形式优先级最高的是恰巧被定义该名称(除非您还指定-a,在这种情况下,所有的命令形式报道) -而不是专门报告在操作数上作为函数。
The -foption doesn't change that - it only includesshell functions in the lookup.
该-f选项不会改变这一点 - 它只在查找中包含shell 函数。
Aliases and shell keywords have higher precedence than shell functions, so, in the case at hand, if an alias foowere alsodefined, type -f foowould report the aliasdefinition.
别名和Shell关键字比壳功能的优先级高,因此,在当前情况下,如果一个别名foo都还规定,type -f foo将报告的别名定义。
Note that zshdoes expand aliases in scripts by default (as does ksh, but not bash), and even if you turn alias expansion off first, type -f/ whence -f/ whichstillreport aliases first.
请注意,zsh不会扩大默认脚本别名(一样ksh,但没有bash),即使你把别名扩展关闭第一,type -f/ whence -f/which还是先上报的别名。
回答by Dan Pritts
If you're not quite sure what you are looking for, you can type just
如果你不太确定你在找什么,你可以只输入
functions
and it will show you all the defined functions.
它会显示所有定义的函数。
Note that there are sometimes a LOT of them, so you might want to pipe to a pager program:
请注意,有时它们很多,因此您可能希望通过管道传输到寻呼程序:
functions | less
to undefine a function, use
要取消定义函数,请使用
unfunction functionname

