The Computer Oracle

Finding the definition of a bash function

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 Finding The Definition Of A Bash Function
00:30 Accepted Answer Score 58
00:40 Answer 2 Score 8
00:53 Answer 3 Score 79
01:21 Answer 4 Score 14
01:29 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.