Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running Ubuntu 10.10 with openbox running on top. I noticed today a command called caller, however there is no man page, it does not respond to any input (or --help) and whereis doesn't find it.

Any idea what it is?

share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer
2  
Awesome. Not only did I learn what that command does, I also learn the "type" command. Thanks :) – user10065 Aug 24 '11 at 14:00
1  
TIL bash has a built-in help command – nibot Aug 24 '11 at 17:55
    
Which I could upvote twice for teaching not only the answer but the process as well. – dmckee Aug 24 '11 at 18:36
    
@Muu, @nibot, @dmckee: also type type, type help, help type and help help could be fun to run :) – enzotib Aug 24 '11 at 18:48

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.

share|improve this answer

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

share|improve this answer
    
Thanks though I picked enzotib's answer as he also taught me 'type'. Many thanks though :) – user10065 Aug 24 '11 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.