linux poison RSS
linux poison Email

Bash Script: Get the Information about the caller of the function (backtrace)

Caller Returns  the  context  of  any active subroutine call (a shell function or a script executed with the . or source builtins). caller displays the line number and source filename of the current subroutine call.  If a non-negative integer is supplied as expr,  caller displays  the line number, subroutine name, and source file corresponding to that position in the current execution call stack.  This extra information may be used, for example, to print a stack trace. 

Below is simple bash script which demonstrate the usage of caller ...
feel free to copy and use this code

Source: cat

foo() {
        caller 0
        echo "In function: foo"

echo "Outside of the function"

goo() {
        echo "In function: goo"


Output: ./
Outside of the function
In function: goo
13 goo ./
In function: foo


Post a Comment

Related Posts with Thumbnails