Get function name in KornShell script

I would like to get the function name from this function for logging purposes.

KornShell Function (ksh):

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

Is there something like $0 that returns the name of the script in scripts, but instead provides the name of the function?

+6
source share
4 answers

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

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

(Tested in pdksh.)

+9
source

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

The main thing is that β€œtypeset myvar = abc” inside the function is now a local variable without any possible side effects outside the function. This makes KSH noticeably safer for large shell scripts. The main end is perhaps the non-POSIX syntax.

+6
source

Use the ksh function "function foo ...":

 $ cat foo1 #!/bin/ksh foo3() { echo "\$0=$0"; } function foo2 { echo "\$0=$0"; } foo2 foo3 $ ./foo1 $0=foo2 $0=./foo1 
+5
source

The function below seems to have got its name in both Bash and ksh:

 # ksh or bash function foo { local myname="${FUNCNAME[0]:-$0}" echo "$myname" } # test foo # ... 
0
source

Source: https://habr.com/ru/post/901632/


All Articles