HomeSoftwareGamesMusicForum

Loading shell functions

Manual search: Sometimes only a function name is known and one has to find the corresponding source file. One could do this by iterating over the directories in $PATH and comparing all file names in each directory with the function name. However, this is rather ineffective, as normally only few directories in $PATH contain function files. In ksh there is an special variable FPATH which holds only directories containing functions. The shell uses it for function autoloading (see below). Obviously it is more efficient to iterate over $FPATH than over $PATH, and it makes sense to use the scheme in bash as well. The variable has no special meaning there, but that doesn't matter. I recommend a setup in both shells similar to this example:
export FPATH=/usr/share/functions/general:/usr/meier/funcs"
PATH+=":$FPATH"
The outer search loop would then start like this:
IFS=:; for dir in ${FPATH:=$PATH}; do ...
I.e., $FPATH is used for function lookup, if set, $PATH otherwise. That's what happens in vared and in the following little function loadfunc. The function is called with a list of function names and tries to find and load all of them. If called with -a, all functions reachable via $FPATH are loaded.
up
Created 2011-08-11 by mopcoge