I want to search all defined functions in bash for some search string. The below is a start, but I want to then eliminate all terms that are not followed by whitespace on the next line (i.e. eliminate all entries that did not find $1
in the body of that function).
fu() { declare -f | grep -e \(\) -e $1; }
e.g. This output:
... tt () untargz () urlfix () ver () [ -f /etc/lsb-release ] && RELEASE="$(cat /etc/lsb-release | grep DESCRIPTION | sed 's/^.*=//g' | sed 's/\"//g') "; vi. () vi.su () ...
would reduce to
... ver () [ -f /etc/lsb-release ] && RELEASE="$(cat /etc/lsb-release | grep DESCRIPTION | sed 's/^.*=//g' | sed 's/\"//g') "; ...
An even much much better way (if possible) would be if every matching function could be determined and displayed in full.
I envision that roughly as:
- Collect the names of the functions with the search string in their body (the name of the function is always a single word on a line before the match, starting at
^
followed by a space then the line ending with()$
), then usingcommand -V
on each of those names, OR, doing adeclare -f
again but this time, using those names and matching everything after them from{
to}
(where{
and}
are on single lines by themselves at^
- I know that grep/awk/sed can do amazing things to those that have such knowledge.
End result would be running fu awk
and it will show me the definition of every function that contains awk
in the body of the function.