#Bash 的 caller 命令
caller [N]
功能
返回当前子程序调用的上下文。
类型
Bash 内置命令。
参数
N- 栈帧索引,0 表示栈顶;省略此参数时只打印文件名,不打印函数名
#示例
#!/bin/bash
func1() {
caller # 调用 func1 的位置:行号+文件名
caller 0 # 调用 func1 的位置:行号+文件名+函数名
caller 1 # 调用 “调用 func1 的函数” 的函数
}
func2() {
func1
}
func2
运行结果:
10 ./demo.sh
10 func2 ./demo.sh
13 main ./demo.sh
#手册
caller: caller [expr]
Return the context of the current subroutine call.
Without EXPR, returns "$line $filename". With EXPR, returns
"$line $subroutine $filename"; this extra information can be used to
provide a stack trace.
The value of EXPR indicates how many call frames to go back before the
current one; the top frame is frame 0.
Exit Status:
Returns 0 unless the shell is not executing a shell function or EXPR
is invalid.