How to show the definition of a zsh function (for example, bash "type myfunc")?

How to show function definition in zsh? type foo does not give a definition.

In bash:

 bash$ function foo() { echo hello; } bash$ foo hello bash$ type foo foo is a function foo () { echo hello } 

In zsh:

 zsh$ function foo() { echo hello; } zsh$ foo hello zsh$ type foo foo is a shell function 
+43
function bash definition zsh
Jul 13 2018-12-21T00:
source share
4 answers

The zsh whence hierarchy, the -f flag prints function definitions:

 zsh$ whence -f foo foo () { echo hello } zsh$ 

In zsh, type is defined as the equivalent of whence -v , so you can continue to use type , but you need to use the -f argument:

 zsh$ type -f foo foo () { echo hello } zsh$ 

And finally, in zsh which is defined as the equivalent of whence -c is the result of printing in a csh-like format, so which foo will give the same results.

man zshbuiltins for all this.

+55
Jul 13 2018-12-12T00:
source share

I always used which for this.

+13
Jul 13 2018-12-12T00:
source share

If you are not completely sure what you are looking for, you can enter only

 functions 

and he will show you all the specific functions.

Please note that sometimes there are a lot of them, so you can connect to the pager program:

 functions | less 

to define a function, use

 unfunction functionname 
+5
May 21 '13 at 15:12
source share

TL; DR

 declare -f foo # works in zsh and bash typeset -f foo # works in zsh, bash, and ksh 



type -f / whence -f / which suboptimal in this case , because their goal is to inform the command form with the highest priority, this is determined by this name (unless you also specify -a , in which case all command forms are reported) - unlike from specific operand reporting as a function.

The -f does not change this - it includes only shell functions in the search.

Aliases and shell keywords take precedence over shell functions, so if the alias foo also defined, type -f foo will report the definition of the alias.

Note that zsh by default expands aliases in scripts (like ksh but not bash ), and even if you first disable aliases, type -f / whence -f / which still provide aliases.

+4
Nov 13 '14 at
source share



All Articles