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 [n [m]]
PARAMETERS
n
Stack frame depth (0 for current/immediate caller, defaults to 0)
m
Number of source lines to print from nth frame (optional)
DESCRIPTION
The caller command is a powerful Bash shell builtin designed for introspection of the execution stack within functions or sourced scripts. It outputs contextual information about the calling frame, including line number, source filename, and optionally source code lines. This is essential for debugging complex Bash scripts, implementing custom stack traces, or dynamic error reporting.
When called without arguments, caller prints three tab-separated fields for the immediate context (frame 0): the line number where the function was invoked, the source file path, and the function name (or main at top level). Specifying one argument n (where n > 0) retrieves info for the nth previous caller up the stack. With two arguments n m, it displays m lines of source code starting from the line of the nth caller.
In modern Bash (4.0+), a leading + prefix like caller +n restricts output to shell functions only, excluding sourced scripts. This feature enhances precision in nested environments. Usage is straightforward within functions:
function debug() { caller 0; }
Enabling set -x complements it for verbose tracing. Note: output format is machine-parsable, aiding scripted analysis. Limitations include unavailability outside Bash functions/scripts and version-specific behaviors.
CAVEATS
Bash builtin only (no standalone binary); requires Bash 3.0+; ineffective at top-level shell (no stack); tab-separated output; +n variant (Bash 4+) limits to functions.
BASIC EXAMPLE
function foo() {
caller
}
foo
# Output: 2 /path/to/script.sh foo
STACK TRACE EXAMPLE
function bar() { foo; }
function foo() { caller 1; }
bar
# Output shows bar's context: e.g., 3 /path/script.sh bar
SOURCE LINES
caller 0 3
# Prints 3 lines of source from current frame's call site.
HISTORY
Introduced in Bash 3.0 (February 2005) for basic stack info; enhanced in Bash 4.0 (2010) with +n prefix for function-only reporting and multi-line source output.


