I work in an environment that has a lot of legacy shell script magic lying around. One thing used heavy from the command line are bash functions that get sourced from some file included from some file included from some file ... included in my .bash_profile. Is there a way to get the definition or even better the location of the definition of these functions without tracking them down through 5 levels of includes?

5 Answers 5


To get the function definition:

type -a function_name
  • @iconoclast: Yes. Jan 22, 2016 at 19:35
  • If you're ever in Grand Rapids I'll buy you one in person... otherwise we can find some other way...
    – iconoclast
    Jan 23, 2016 at 21:05
  • 1
    whence -f function_name in zsh
    – dza
    Sep 13, 2017 at 13:17

Assuming you have a function named foo the commands below will get the location of the function's definition, that is it will get the name of the file in which the function is defined as well as the line number at which the function is defined within that file.

# Turn on extended shell debugging
shopt -s extdebug

# Dump the function's name, line number and fully qualified source file  
declare -F foo

# Turn off extended shell debugging
shopt -u extdebug

In my case the output of these commands is:

foo 32 /source/private/main/developer/cue.pub.sh
  • 4
    Perfect. I just made this into a bash function in my bashrc file. whereisfunc() {shopt -s extdebug;declare -F $1;shopt -u extdebug}
    – Neil
    Feb 12, 2015 at 3:49
  • 7
    @Neil: That's handy, thanks: let me suggest using a subshell to localize the effect of shopt without changing the current shell's state: whichfunc () ( shopt -s extdebug; declare -F "$1"; ) (note the use of (...) instead of {...}).
    – mklement0
    Mar 7, 2016 at 21:43
  • Why those commands don't work from a script? function works correctly
    – albfan
    Oct 7, 2017 at 7:35
  • 2
    This should be the accepted answer.
    – Ryan
    Dec 13, 2017 at 19:00

To see the definition of the function (as opposed to where it came from), use:

declare -f <functionname>
  • 1
    I think this one is better than type -a because it doesn't print the annoy locale dependent first line~~
    – yuyichao
    Mar 16, 2012 at 1:50
  • 1
    but since there's no location given this is an answer to an unasked question—at least unasked here
    – iconoclast
    Jan 22, 2016 at 19:29
  • I found this while Googling how to know the defintion of a function ("find definition" is kind of ambiguous) so this answer was useful to me.
    – LondonRob
    Jun 3, 2021 at 10:19

bash -x will dump what bash is running as it starts up, which should let you trace it more easily. Don't forget to exit the newly-opened shell.

  • 1
    Handy stuff; to capture the trace in a file, use script out.txt bash -x (again, don't forget to exit).
    – mklement0
    Mar 8, 2016 at 2:43

another way, that I find even simpler lately:

  which <functionname>

(this will also show whether it's an alias or a script, but will not track down the source of an alias)

  • 2
    I don't think which gives information about aliases on BSD or mac. Also, this question is about functions, and which does not give information about functions.
    – dbn
    Apr 8, 2013 at 20:45
  • 1
    Thanks for that note. I see now that "which" only resolves functions in zsh, not bash. Apr 9, 2013 at 1:57
  • 2
    I think this answer is likely to invite downvotes... it doesn't seem to address the question at all, since there's no information on location.
    – iconoclast
    Jan 22, 2016 at 19:28
  • I've found it useful for the purpose of tracking down the declaration to use this and find out whether it's an alias or function, and to know the syntax with which it was declared. It's true though that only @HairOfTheDog's answer gives the file directly. Apr 26, 2016 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .