Finding the definition of a bash function

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

To get the function definition:

type -a function_name

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

declare -f <functionname>

Tags:

Shell

Bash