Caller Command in Linux: 7 Proven Ways to Fix Bash Bugs
The caller command in Linux is a Bash builtin that tells a function where it was called from: the line number, the calling function and the script file. It is the quickest way to turn "something failed somewhere" into a real stack trace in a shell script. The previous version of this article showed output that Bash does not produce (for example a line number 0 for a plain call), so everything below was rewritten from scratch and re-run.
All examples ran on 2026-10-30 in a debian:13 container with GNU bash 5.2.37. Each script is short enough to copy, and each result is the exact output of that run.
Contents
What the caller command prints
The syntax is caller [expr]. Without an argument it returns the line number and source file of the code that called the current function. With a number it walks up the call stack and also prints the function name. Start with the smallest possible example:
#!/bin/bash
function example {
caller
}
example
$ bash ex1.sh
5 ex1.sh
[rc=0]
The function was called on line 5, which is the example line at the bottom. The old article claimed the answer was "0 ./script.sh"; that is wrong, because the caller command reports the real line of the call. Remember that the file name is printed exactly as you started the script, so bash ex1.sh gives ex1.sh and ./ex1.sh gives ./ex1.sh.
caller 0, caller 1 and the whole stack
The argument is a frame number. Frame 0 is the current function, frame 1 is the function that called it, and so on. The plain form prints two fields, the numbered forms print three (line, function, file). Here is a three level chain:
#!/bin/bash
level1() { level2; }
level2() { level3; }
level3() {
echo "caller : $(caller)"
echo "caller 0 : $(caller 0)"
echo "caller 1 : $(caller 1)"
echo "caller 2 : $(caller 2)"
caller 3 || echo "caller 3 -> rc=$?"
}
level1
$ bash ex3.sh
caller : 3 ex3.sh
caller 0 : 3 level2 ex3.sh
caller 1 : 2 level1 ex3.sh
caller 2 : 11 main ex3.sh
caller 3 -> rc=1
Read it from the bottom: caller 2 says that level1 was called from line 11 by main, which is the top level of the script. caller 3 asks for a frame that does not exist, so it prints nothing and returns 1. That return value is what makes a loop over all frames possible:
#!/bin/bash
logCaller() {
local frame=0
while caller $frame; do
((frame++))
done
}
funcA() { funcB; }
funcB() { funcC; }
funcC() { logCaller; }
funcA
$ bash ex4.sh
10 funcC ex4.sh
9 funcB ex4.sh
8 funcA ex4.sh
11 main ex4.sh
The frame numbers do not include logCaller itself in the output because frame 0 describes the call into logCaller: it was called from line 10 inside funcC. The old article listed line numbers that did not match its own script.
The same data in BASH_LINENO, FUNCNAME and BASH_SOURCE
The caller command is a friendly view of three arrays that Bash keeps up to date. You can read them directly when you want to build your own message:
$ bash ex5.sh
FUNCNAME : g f main
BASH_LINENO: 2 8 0
BASH_SOURCE: ex5.sh ex5.sh ex5.sh
The arrays line up by index: g was called from line 2 (inside f), f from line 8, and main has no caller, so its line is 0. Use caller in functions and loops because it is one word, and use the arrays when you only need one field.
A die() helper that prints a stack trace
This is the practical use of the caller command: an error function that shows the whole path to the failure and exits with a chosen code.
#!/bin/bash
die() {
local code=${2:-1} i=0 line func file
echo "ERROR: $1" >&2
while read -r line func file < <(caller $i); do
echo " at $func ($file:$line)" >&2
((i++))
done
exit "$code"
}
check_file() { [[ -r $1 ]] || die "cannot read $1" 3; }
load_config() { check_file /etc/app.conf; }
load_config

$ bash ex6.sh; echo "rc=$?"
ERROR: cannot read /etc/app.conf
at check_file (ex6.sh:11)
at load_config (ex6.sh:12)
at main (ex6.sh:13)
rc=3
The loop stops by itself when caller $i prints nothing, because read then gets end of file. Line 11 is where die was called inside check_file, which is exactly where you start debugging.
Combining the caller command with trap ERR
An ERR trap runs when a command fails, and the caller command in the trap tells you where:
#!/bin/bash
set -E
trap 'echo "ERR in $(caller)" >&2' ERR
backup() { cp /nonexistent /tmp/x; }
backup
echo "not reached with set -e"
$ bash ex7.sh; echo "rc=$?"
cp: cannot stat '/nonexistent': No such file or directory
ERR in 5 ex7.sh
ERR in 0 NULL
not reached with set -e
rc=0
Two things are worth knowing. The trap fired twice: once for cp and once more when backup returned a non-zero status at the top level, where the caller command prints 0 NULL. And the script kept running, because this example uses set -E (inherit the trap into functions) but not set -e. Add set -eE if you want the script to stop on the first failure.
Sourced files, extdebug and common errors
When a file is loaded with source, the caller command reports source as the calling "function" and the file that did the sourcing:
$ bash ex8.sh
2 source ./lib.sh
With shopt -s extdebug Bash also fills BASH_ARGV with the arguments of every frame, innermost first, so a function called as g one two shows them reversed:
$ bash ex11.sh
3 f ex11.sh
args: two one
The errors you will meet are all small. A non-number gives an error and exit code 2, a frame that does not exist gives nothing and exit code 1, and at the top level of bash -c there is no frame, so it also returns 1:
$ bash -c 'f() { caller abc; }; f'; echo "rc=$?"
environment: line 1: caller: abc: invalid number
caller: usage: caller [expr]
rc=2
$ bash -c 'f() { caller 99; }; f'; echo "rc=$?"
rc=1
$ bash -c 'caller; echo "rc=$?"'
rc=1
The caller command is a builtin of Bash only. In dash, the usual /bin/sh on Debian and Ubuntu, it does not exist, so a script with #!/bin/sh fails:
$ sh -c 'f() { caller; }; f'; echo "rc=$?"
sh: 1: caller: not found
rc=127
Use #!/bin/bash in any script that relies on it, and see help caller or the Bash Builtins section of the GNU manual for the official description.
Quick checklist for the caller command
- Plain
caller: line and file of the current function's caller.caller N: line, function and file of frame N. - Loop
while caller $ito print the whole stack; it ends by itself with exit code 1. - Put it in an
ERRtrap or adiefunction so every failure reports its location. - Use
#!/bin/bash, not#!/bin/sh; addextdebugonly when you needBASH_ARGV.
For another Bash scripting pattern tested the same way, read the bash yes no prompt guide with confirm functions, timeouts and unattended runs.
Comments
Post a Comment