/bin/sh 中的 $FUNCNAME 替代项

$FUNCNAME alternative in /bin/sh

POSIX shell 是否有类似于 $FUNCNAME in bash 的内容?

No. POSIX sh 标准 is quite small 要求具有特殊行为的变量列表 is quite small,不等同于 [=10] =] 已给出;最接近的是 LINENO.

(如果 POSIX sh 中已经存在这样的东西,为什么 bash 会添加自己的而不是实现规范?)

编辑 请注意我如何测试是否有一个变量告诉我当前函数是什么。最初令人困惑的是 FUNCNAME 在 Mac 上可用,甚至在 /bin/sh 上也可用,所以我想知道为什么它在 Linux 上是空的——因此是 OP。 在 variables 打开规范对其有所了解(请参阅已接受的答案),但我仍然很好奇函数中还有什么可用的。

如何查看函数相关环境shell变量:

set shall write the names and values of all shell variables in the collation sequence of the current locale

所以

$ set > a

$ function f() {
    set > b
}

$ f

然后

diff a b

将在 bash 中提供与函数相关的变量(如预期的那样在 sh 中没有任何内容)

> BASH_LINENO=([0]="5")
14a15
> FUNCNAME=([0]="f")
55c56