'Show call stack in Bash

To start with, I do not know if this is something trivial and common knowledge; I don't know, so am asking here.

I am running a 3rd party app and trying to nail down a problem for which I need to find out the call stack.

When I call a Bash script, it calls a number of other scripts and binaries, processing stuff and exits.

What I need is a way of finding out exactly which scripts and binaries it called. pstree shows a stack but only for a process that is currently executing.



Solution 1:[1]

Run the script with bash -x, which will produce output on standard error to show each command that is executed.

Solution 2:[2]

function stacktrace { 
   local i=1 line file func
   while caller $i | read line func file; do
      echo >&2 "[$i] $file:$line $func(): $(sed -n ${line}p $file)"
      ((i++))
   done
}

from https://gitlab.com/kyb/autorsync/-/blob/master/utils.bash#L84

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner
Solution 2