This is my simplified script.

I am wondering if the proc() can know if it is run directly or through the runner.

runner () {
proc() {
    eval 'version=$(echo "SUCCESS: **** ${BASH_VERSION} ****")'
    echo -e "$version"; 
    return 0

runner proc

What do you think?


proc is not a separate process in your example. It's just a function, run in the same process as the main shell.

The $FUNCNAME array gives it access to its backtrace:

foo(){ bar; }
bar(){ baz; }
baz(){ proc; }
proc(){ echo "${FUNCNAME[@]}"; }

$ foo
proc baz bar foo main

So yes, it can:

case ${FUNCNAME[1]} in runner) ...

If you experiment with it, you will see that running it in a subshell / subprocess doesn't break the backtrace or affect it in any way:

foo(){ (bar &) | cat; }
=> same output
  • true, I clarified my title. let me check what I can do with your info. thanks a bunch – conanDrum Jun 14 at 0:02
  • I've added an example test and a note about functions, backtraces & subprocesses – mosvy Jun 14 at 0:14
  • excellent mosvy thanks mate – conanDrum Jun 14 at 0:37
  • I tested with calling a function like this 'bash <filename> <function>' and like this 'runner bash <filename> <function>' and the function is oblivious to who is calling it, based on your suggestion. Any other suggestions in this case? – conanDrum Jun 14 at 6:35
  • The backtrace will not work through exec, ie. processes which start an external command. For that to work, Unix will have to be something like a Lisp Machine ;-) – mosvy Jun 14 at 6:42

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.