Bash – assign and inspect bash function metadata

bashfunction

I often generate and register a lot of bash functions that automate many of the task I usually do in my development projects. That generation depends on the meta-data of the project I am working on.

I want to annotate the functions with the info of the project they were generated, this way:

func1() {
# This function was generated for project: PROJECT1
echo "do my automation"
}

Ideally, I would be able to see the comment when I inspect the definition:

$ type func1

func1 is a function
func1 () 
{
    # This function was generated for project: PROJECT1
    echo "do my automation"
}

But somehow bash seems to ignore the comments at the moment of loading the function, not when executing it. So the comments are lost and I get this result:

func1 is a function
func1 () 
{
    echo "do my automation"
}

Is there any way to assign metadata to functions, and check them afterwards? It is possible to retrieve it when inspecting the definition with type?

Best Answer

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
}