find file containing a function

1

I would like to know a way to get the file that contains a function that is accessible because it's been sourced in shell. For Bash and ZSH.

For example when I want to find out the source of function git_showhidden it prints the function but not the file.

$ which git_showhidden
git_showhidden () {
    files=($(git ls-files -v | grep "^[[:lower:]]" | sed 's/h //'))
    echo "${files[@]}"
}

Of course I could search for the string, but that is boring and slow. which, whence and type do not deliver what I need.

d-nnis

Posted 2017-04-14T22:40:37.723

Reputation: 11

There is no record in bash of the file where a function, variable or alias is defined. You need to look through the files that are executed during the start-up of an interactive bash session: /etc/profile,/etc/profile.d/*, /etc/bash.bashrc, $HOME/.profile and $HOME/.bash*, though I'm not sure this is an exhaustive list; zsh will have a similar set of files. Alternatively, if an installation created the function, look at the files installed by your git package(s), which should include where your function is defined. – AFH – 2017-04-15T01:00:26.287

Talking about the startup order of Shell files this might be a good hint. And remembering that a function knows everything, that a parent does (as it's been stated here, I just might have to search the parents for evidence... (The function quoted above serves merely as a sample to show a typical output of which in that case.)

– d-nnis – 2017-04-15T07:08:19.763

I was hoping that there is an easier way to find out. – d-nnis – 2017-04-15T07:24:07.380

A similar question focusing on zsh: https://superuser.com/q/707354/195224 and according to Wiil's answer there: whence -v git_showhidden should do what you want.

– mpy – 2017-04-15T12:25:18.543

Thanks for the link. As for whence: I seem to have to wait for the next release (via Debian). On ZSH 5.0.7 I just get git_showhidden is a shell function. – d-nnis – 2017-04-15T17:39:45.313

Interesting links. If you run bash -x you will see all the commands executed on start-up. I haven't found any way in Ubuntu to log the commands to a file, as redirection or piping to tee or less suppresses the debug information; so you need a big screen buffer in order to capture on screen the whole log, which you can then select and copy to a file for browsing and searching. – AFH – 2017-04-15T18:42:03.843

The output of zsh -x could get logged with zsh -x > xzsh.log 2>&1. That is already quite helpful. This does not work for bash though. – d-nnis – 2017-04-16T21:56:05.153

Answers

0

Only part of an answer, but for , type does exactly what you need.

Example:

$ type extract
extract is a shell function from /usr/share/oh-my-zsh//plugins/extract/extract.plugin.zsh

You may pipe its output to sed "s/.*is a shell function from //g" to get the path only (this may have some issues, but should work for most of the output).

styrofoam fly

Posted 2017-04-14T22:40:37.723

Reputation: 1 746