I want to generate a list of all functions in a shell script (a shell script function library that I am sourcing). Currently, I am experimenting with typeset in ways such as the following:
typeset -f
typeset -F
typeset -F | awk -F"declare -f " '{print $2}' | grep -v '_'
A difficulty I am having is that some functions which are not in the function library are listed and I don't know how to excise these from the function listing or how to generate the function listing in a better way. I would welcome any ideas you may have on this.
A bonus would be to generate a function listing in the order in which the functions appear in the shell script.
I assume bash is the shell. typeset (or declare ) show the current environment, functions are part of the environment:
You could experiment with "env" to start your script in a clean environment, try:
env -i "PATH=$PATH" "TERM=$TERM" "LANG=$LANG" myscript.sh
as starting point.
The best way to enumerate functions in bash is documented here: Where is function's declaration in bash? Note that "declare -F" does not normalise the filename, it will be as invoked/sourced.
while read xx yy fn; do
func=( $(shopt -s extdebug; declare -F $fn) )
printf "%-30s %4i %-30s\n" "${func[@]:2}" "${func[1]}" "${func[0]}"
done < <(declare -F)
All you need to do is filter by the required filename, and sort by line number.