assign and inspect bash function metadata
function func_name()
{
: '
Invocation: func_name $1 $2 ... $n
Function: Display the values of the supplied arguments, in double quotes.
Exit status: func_name always returns with exit status 0.
' :
local i
echo "func_name: $# arguments"
for ((i = 1; i <= $#; ++i)); do
echo "func_name [$i] \"$1\""
shift
done
return 0
}
Yes, type
seems to only print out the parts of a function that will be run. This seems reasonable to me, really, since usually that's all you are interested in when querying type
.
As a workaround, instead of using comments, add your meta data like this:
func1() {
meta="This function was generated for project: PROJECT1"
echo "do my automation"
}
There's no need to ever actually use that variable, but it will appear when querying the function with type
:
$ type func1
func1 is a function
func1 ()
{
meta="This function was generated for project: PROJECT1";
echo "do my automation"
}
You can use the nop builtin :
. Besides, you don't need to store it as a variable:
function f() {
: your metadata here
: "or here"
# do yours
}
EDIT: Beware of special characters into your metadata. For pure text, you can use:
: <<EOT
Your metadata text here.
EOT
EDIT: You may use instead a global associative array to store all function's metadata:
declare -A METADATA=()
METADATA[fun1]='foo bar'
function fun1() {
echo I have some metadata: "${METADATA[$FUNCNAME]}"
}
METADATA[fun2]='baz you'
function fun2() {
echo I have some other metadata: "${METADATA[$FUNCNAME]}"
}
This way, you don't need to parse declare
or type
's output, but only query for an array's key.