The Computer Oracle

Finding the definition of a bash function

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Track title: CC E Schuberts Piano Sonata D 784 in A

--

Chapters
00:00 Question
00:41 Accepted answer (Score 57)
00:53 Answer 2 (Score 78)
01:29 Answer 3 (Score 14)
01:44 Answer 4 (Score 7)
02:01 Thank you

--

Full question
https://superuser.com/questions/144772/f...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #shell

#avk47



ANSWER 1

Score 79


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



ACCEPTED ANSWER

Score 58


To get the function definition:

type -a function_name



ANSWER 3

Score 14


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

declare -f <functionname>



ANSWER 4

Score 8


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.