caller
Display function call information (stack frame)
TLDR
Print the line and filename where the current function was called
Print the line, function and filename where the current function was called
Print the line, the function name and the filename of a function call n frames back
SYNOPSIS
caller [expression]
PARAMETERS
expression
An arithmetic expression to be evaluated and printed with the return value. If expression is supplied the result will be printed. Default is no expression.
DESCRIPTION
The caller command is a shell built-in that outputs the context of the current subroutine call. Specifically, it displays the line number and calling subroutine (or filename if called from the top level) of the current execution context. This is a valuable debugging tool, especially within scripts containing functions. It helps trace the function call stack and identify where a specific function was invoked from. By default, caller displays the line number and function or source filename. It's useful for tracking the flow of execution in complex scripts. The behavior can vary slightly between different shells.
CAVEATS
The output format and availability of the 'expression' feature may vary depending on the shell being used (e.g., bash, zsh). caller might not provide meaningful results outside of functions or when called directly from the command line.
EXIT STATUS
Returns success unless caller is used outside of a function or subroutine call.
EXAMPLES
Example:
function my_function { caller; }
my_function
Outputs the line number and file where my_function was called from.
Example 2:
function my_function { caller 0 "example expression"; }
Prints the result of the expression.
HISTORY
The caller command has been a part of shells like bash for many years, providing a simple yet effective mechanism for debugging shell scripts and tracing the execution flow. It's designed to give a snapshot of the call stack at a specific point in the script's execution.