94

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
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
  • 1
    `declare -f foo` is the better choice even in bash - and it works in `zsh` too; see [my answer](http://stackoverflow.com/a/26911615/45375) for background. – mklement0 Nov 13 '14 at 14:53

4 Answers4

129

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    "whence" just returns the name of the function. It does not show the function definition. – Rob Bednark Jul 13 '12 at 21:38
  • 2
    @RobBednark look more closely: `-f` is used in the answer, which does print function definitions – pb2q Jul 13 '12 at 21:39
  • `type -f foo` also does this; `type` is equivalent to `whence -v`. – Keith Thompson Jul 13 '12 at 21:48
  • I see, "whence -f foo" does give the behavior that I'm looking for. Equivalent to Thor's "which foo". – Rob Bednark Jul 13 '12 at 21:49
  • @pb2q, nice details for the 3 different ways, as well as the reference. Thanks! – Rob Bednark Jul 14 '12 at 13:53
  • Works, but only if no alias of the same name happens to be defined, in which case that is reported (`whence -f`, `type -f`, `which` report the highest-precedence form of the command). – mklement0 Nov 13 '14 at 14:55
  • Warning to other RHEL6/Centos6 users: I'm picking up an alias for `which` from `/etc/profile.d/which2.sh`, which comes from the `which` package. – Dan Pritts Dec 09 '14 at 03:04
  • declare -f foo (see below) is better because even if the function is hidden by an alias, it will still be shown, whereas 'whence' is liable to show just the alias, OR both the alias and the function. – Ray Andrews Dec 27 '14 at 00:12
  • Bonus: if you use `type`/`whence -v` you will get the filename where the function is defined: for example the command `type rvm_prompt_info` gives `rvm_prompt_info is a shell function from /home/cristian/.oh-my-zsh/lib/prompt_info_functions.zsh` – CristianCantoro Feb 02 '18 at 23:32
31

I've always just used which for this.

Thor
  • 45,082
  • 11
  • 119
  • 130
  • 6
    "which" gives the behavior I'm looking for -- thanks Thor! (Note that "which" is a zsh builtin whereas in bash it calls /usr/bin/which , and /usr/bin/which has different behavior than the zsh builtin function "which") – Rob Bednark Jul 13 '12 at 21:44
  • 2
    Works, but only if no alias of the same name happens to be defined, in which case _that_ is reported (`which` reports the highest-precedence form of the command). – mklement0 Nov 13 '14 at 14:28
24

tl;dr

declare -f foo  # works in zsh and bash

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

If you don't mind or prefer including all command forms that exist for a given name in the output:Thanks, Raine Revere.

type -af  # zsh only (works differently in bash and ksh)

type -f / whence -f / which are suboptimal in this case, because their purpose is to report the command form with the highest precedence that happens to be defined by that name - as opposed to specifically reporting on the operand as a function.

That said, in practice this means that only an alias of the same name takes precedence (and technically also a shell keyword, though naming functions for shell keywords is probably a bad idea anyway).

Note that zsh does 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 / which still report aliases first.

In zsh, the -f option only includes shell functions in the lookup in zsh, so - unless -a is also used to list all command forms - an alias by the given name would print as the only output.

In bash and ksh, type -f actually excludes functions from the lookup; whence doesn't exist in bash, and in ksh it doesn't print the function definition; which is not a builtin in ksh and bash, and the external utility by definition cannot print shell functions.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thanks, @RaineRevere; I've added your suggestion to the top section of the answer and added a clarification to the bottom section. – mklement0 Jan 23 '21 at 20:26
9

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
Dan Pritts
  • 1,274
  • 16
  • 14
  • 2
    +1; Note that `functions` is equivalent to `typeset -f` (except for the `-M` option), so you can even use the grammatically slightly counter-intuitive command `functions foo` to get information about a _given_ function. – mklement0 Nov 13 '14 at 14:58