1

Say I have this:

foo(){
   install_ores_gitflow;
   command foo "$@"
}

what I want to do, is dynamically grab the function name, something like this:

foo(){
   install_ores_gitflow;
   command "$0" "$@"
}

where "$0" would evaluate to "foo" is this possible somehow, I am looking for something reliable.

3

How about the FUNCNAME shell variable? man bash:

FUNCNAME

An array variable containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function.

| improve this answer | |
  • How to use it given the OP? can you show an example? – Alexander Mills Aug 31 '18 at 21:21
  • foo() { command "$FUNCNAME" "$@"; } – glenn jackman Aug 31 '18 at 21:29
  • what a minute, is $FUNCNAME an array or just a string? how do I access just the first element of the array? – Alexander Mills Sep 1 '18 at 18:08
  • ` Referencing an array variable without a subscript is equivalent to referencing the array with a subscript of 0. ` (also from man bash) – RudiC Sep 1 '18 at 18:18
  • Ahh that is cool, but if I wanted to be explicit, it would be this right "${FUNCNAME[0]}", is there a way to do it shorter with "$FUNCNAME[0]"? – Alexander Mills Sep 1 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.