38
在BASH中,是否可以在函數體中獲取函數名?以下面的代碼爲例,我想在它的主體中打印函數名稱「Test」,但「$ 0」似乎是指腳本名稱而不是函數名稱。那麼如何獲得函數名?在BASH中,是否可以在函數體中獲取函數名?
#!/bin/bash
function Test
{
if [ $# -lt 1 ]
then
# how to get the function name here?
echo "$0 num" 1>&2
exit 1
fi
local num="${1}"
echo "${num}"
}
# the correct function
Test 100
# missing argument, the function should exit with error
Test
exit 0
謝謝,這真的有幫助。我學習的不僅僅是我的問題的解決方案。腳本失敗時,可以使用此數組打印調用堆棧。 – 2012-02-05 03:33:41
當然。在這方面,你也可能會發現'BASH_LINENO'的內容值得關注。 – FatalError 2012-02-05 03:35:38
或者您可以使用較短和等效的$ FUNCNAME。 – 2017-10-06 16:34:56