What is the "caller" command?

Run

type caller

and you will see it is a shell built-in. Running

help caller

will show its function, reported as well in bash's manual page. Briefly

Return the context of the current subroutine call.


The caller is builtin command (not specified by POSIX) appeared in Bash version 3.0 and it returns the context of any active subroutine call. See: Bash-Builtins for more reading.

Syntax:

caller [FRAMENUMBER]

If frame number is supplied as a non-negative integer, it displays the line number, subroutine name, and source file corresponding to that position in the current execution call stack.

Without any parameter, caller displays the line number and source filename of the current subroutine call.

Check the following Simple stack trace at Bash Hackers Wiki:

#!/bin/bash

die() {
  local frame=0
  while caller $frame; do
    ((frame++));
  done
  echo "$*"
  exit 1
}

f1() { die "*** an error occured ***"; }
f2() { f1; }
f3() { f2; }

f3

Output:

12 f1 ./callertest.sh
13 f2 ./callertest.sh
14 f3 ./callertest.sh
16 main ./callertest.sh
*** an error occured ***

Here is sample of a decent die function to track down errors in moderately complex scripts:

{ bash /dev/stdin; } <<<$'f(){ g; }\ng(){ h; }\nh(){ while caller $((n++)); do :; done; }\nf'

For more sophisticated debugging, Bash extended debugging features are available and a number of special parameters that give more detail than caller (e.g. BASH_ARG{C,V}). Tools such as Bashdb can assist in using some of Bash's more advanced debug features.


It's a shell builtin command: man bash (Then search for 'caller')
It can be used to print a stack trace.