Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
caller          # "LINE FILE"
caller 0        # "LINE FUNCTION FILE" for the current function

Try it

Bash Where was this function called from?

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.

Bash A stack trace and an error helper

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

Terminal
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 0 inside inner gives the line where inner was invoked, with the function name inner.
  • Loop until failure. while caller "$i" is the idiomatic way to walk the whole stack, as in the example.
  • main is the outermost frame. When a script is run, the top-level code appears as main, so a loop with caller "$i" ends with a main line, as in the example.
  • Subshells and command substitution. $(caller) runs in a subshell but inherits the call stack, so it works there; in a pipeline caller still sees the frames of the enclosing function.
  • Arrays are richer. FUNCNAME[i] is the function at frame i, BASH_SOURCE[i] its file and BASH_LINENO[i-1] the call line. These arrays exist only while a function is running.
  • Combine with traps. trap 'echo "failed at line $LINENO"' ERR and set -E plus caller produce useful error reports; see trap.
  • Needs extdebug? Not for this. caller works without any option; shopt -s extdebug only adds more detail for debugging and function tracing.
  • Debugging aid. Leave stack printing out of production output, or send it to standard error.