Linux Command Reference ยท Shell Builtins
Bash caller command
Reference for the Bash caller builtin: print the line, function and file of the current call frame, build stack traces, and relate it to FUNCNAME and BASH_LINENO.
caller reports where the current function was called from. Without an argument it prints the line number and file name of the call site; with a number N it walks up N frames and also prints the name of the function at that level. It is the building block for error handlers and stack traces in larger Bash scripts.
It is most useful inside a function or a sourced script. At the top level of a running script it reports the script's own frame (0 NULL), and in an interactive shell with no function active it prints nothing and returns 1. Bash also keeps the same data in the arrays FUNCNAME, BASH_LINENO and BASH_SOURCE, which you can read directly.
caller # "LINE FILE" caller 0 # "LINE FUNCTION FILE" for the current function
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
caller [FRAME]
| Name | Description |
|---|---|
FRAME |
Optional non-negative number. 0 is the function that is running, 1 the one that called it, and so on. |
Output: without FRAME: LINE SOURCEFILE. With FRAME: LINE SUBROUTINE SOURCEFILE. The line is where that frame was called from, not where it is defined.
Returns: 0 when the frame exists; 1 when there is no such frame (for example caller 5 in a shallow stack, or any call with no frames in an interactive shell).
Usage notes
- Line numbers refer to the call.
caller 0insideinnergives the line whereinnerwas invoked, with the function nameinner. - Loop until failure.
while caller "$i"is the idiomatic way to walk the whole stack, as in the example. mainis the outermost frame. When a script is run, the top-level code appears asmain, so a loop withcaller "$i"ends with amainline, as in the example.- Subshells and command substitution.
$(caller)runs in a subshell but inherits the call stack, so it works there; in a pipelinecallerstill sees the frames of the enclosing function. - Arrays are richer.
FUNCNAME[i]is the function at framei,BASH_SOURCE[i]its file andBASH_LINENO[i-1]the call line. These arrays exist only while a function is running. - Combine with traps.
trap 'echo "failed at line $LINENO"' ERRandset -Epluscallerproduce useful error reports; seetrap. - Needs
extdebug? Not for this.callerworks without any option;shopt -s extdebugonly adds more detail for debugging and function tracing. - Debugging aid. Leave stack printing out of production output, or send it to standard error.