Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function name in KornShell script

I'd like to get the function name from within the function, for logging purposes.

KornShell (ksh) function:

foo ()
{
    echo "get_function_name some useful output"
}

Is there anything similar to $0, which returns the script name within scripts, but which instead provides a function's name?

like image 830
Dima Avatar asked Nov 16 '11 16:11

Dima


2 Answers

If you define the function with the function keyword, then $0 is the function name:

$ function foo {
>     echo "$0"
> }
$ foo
foo

(Tested in pdksh.)

like image 139
Fred Foo Avatar answered Sep 28 '22 11:09

Fred Foo


[...] what are the main pros/cons of using keyword function?

Main pro is that "typeset myvar=abc" inside the function is now a local variable, with no possible side effects outside the function. This makes KSH noticeably safer for large shell scripts. Main con is, perhaps, the non-POSIX syntax.

like image 23
Lee Busby Avatar answered Sep 28 '22 11:09

Lee Busby