Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers

up vote 8 down vote accepted

To get the function definition:

type -a function_name
share|improve this answer
add comment

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
share|improve this answer
add comment

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

declare -f <functionname>
share|improve this answer
    
I think this one is better than type -a because it doesn't print the annoy locale dependent first line~~ –  yuyichao Mar 16 '12 at 1:50
add comment

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.

share|improve this answer
add comment

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)

share|improve this answer
    
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. –  dbw Apr 8 '13 at 20:45
    
Thanks for that note. I see now that "which" only resolves functions in zsh, not bash. –  Joshua Goldberg Apr 9 '13 at 1:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.