Inside a function, how can I get the file name of whatever script called that function?
Via prompt expansion, %x
provides the name of the file that defines the function. %N
provides the function name. How do I get the name of the file that called the function?
This would be useful to report the runtime of each of my startup scripts, e.g.:
~/.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"