在一个函数内部,如何获取调用该函数的脚本文件名?
通过提示展开,%x
提供定义函数的文件名。 %N
提供函数名称。我该如何获取调用该函数的文件名?
这对于报告每个启动脚本的运行时非常有用,例如:
~/.zshenv:
function start_timer() {
start_time=$(date +%s.%N)
}
function stop_timer() {
stop_time=$(date +%s.%N)
elapsed_time=$((stop_time-start_time))
echo "$SCRIPTNAME took $elapsed_time seconds"
}
start_timer
# initialize env...
end_timer # "~/.zshenv took 0 seconds"
~/.zshrc:
start_timer
# initialize interactive shell...
end_timer # "~/.zshrc took 2 seconds"
~/.zlogin:
start_timer
# initialize login shell...
end_timer # "~/.zlogin took 2 seconds"