function::caller - Linux


Overview

function::caller is a bash built-in command that prints the name of the function from which it was called, or an empty string if called from the top level. It is useful for debugging and introspection.

Syntax

function::caller [level]

Options/Flags

  • level: The number of levels up the call stack to go before printing the function name. The default is 1, which prints the name of the immediately calling function. A value of 0 prints the name of the function that called the current function.

Examples

$ function call_stack() {
>   function::caller
>   function::caller 1
>   function::caller 2
> }
$ call_stack
call_stack
function::caller

This script prints the names of the current function and the two functions that called it.

Common Issues

One common issue with function::caller is that it can only print the names of functions defined in the current script. It cannot print the names of functions defined in other scripts or in the shell’s environment.

Integration

function::caller can be used with other bash commands to perform complex tasks. For example, it can be used with the eval command to print the value of a variable from a different scope.

$ var=42
$ function call_stack() {
>   eval "echo \$var"
>   function::caller
> }
$ call_stack
42
call_stack

Related Commands

  • call: Prints the name of the currently executing function.
  • env: Prints the environment variables.
  • set: Prints the values of shell variables.