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.
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 interactivebash
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 yourgit
package(s), which should include where your function is defined. – AFH – 2017-04-15T01:00:26.287Talking 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
– d-nnis – 2017-04-15T07:08:19.763which
in that case.)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:
– mpy – 2017-04-15T12:25:18.543whence -v git_showhidden
should do what you want.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.313Interesting 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 totee
orless
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.843The output of
zsh -x
could get logged withzsh -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