Home > Blockchain >  How to view comments of a function in bash
How to view comments of a function in bash

Time:10-06

To read a function definition, you can use one of the below

typeset -f <func_name>    # OR
declare -f <func_name>    # OR 
type <func_name>

But none of the above commands shows the comments defined inside the function. Is there a command/option which shows comments too along with the definition.

CodePudding user response:

The comments are treated as comments when the function is defined, and are not part of the function itself. But there is a workaround. You can pass arguments to : as a form of comment. eg:

$ foo() { echo foo; : this is a comment; }
$ typeset -f foo
foo () 
{ 
    echo foo;
    : this is a comment
}

CodePudding user response:

Also you could use heredoc. @William answers works for multi line comments as well if surrounded with quotes.

foo() {
    <<'##'
       This is a comment
       more comments
##
echo foo
}

foo() {
    : '
       This is a comment
       more comments
'
echo foo
}
  • Related