How to show zsh function definition (like bash "type myfunc")?

Rob Bednark picture Rob Bednark · Jul 13, 2012 · Viewed 14.6k times · Source

How do I show the definition of a function in zsh? type foo doesn't give the 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

Answer

pb2q picture pb2q · Jul 13, 2012

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

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

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

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

And, finally, in zsh which is defined as equivalent to whence -c - print results in csh-like format, so which foo will yield the same results.

man zshbuiltins for all of this.