Andras P
Andras P

Reputation: 51

$FUNCNAME alternative in /bin/sh

Does the POSIX shell have something similar to $FUNCNAME in bash ?

Upvotes: 3

Views: 1890

Answers (2)

Andras P
Andras P

Reputation: 51

EDIT Just a note on how I tested if there is a variable that tells me what the current function is. What was confusing originally is FUNCNAME is available on Mac even in /bin/sh so I was wondering why it was empty on Linux - hence the OP. Opening the spec at variables shed some light on it (see accepted answer), but I was still curious what else is available in functions.

How to see the function related environment shell variables:

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

So

$ set > a

$ function f() {
    set > b
}

$ f

Then

diff a b

Will give me function related variables in bash (and nothing in sh as expected)

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

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295383

No. The list of variables which are required to have special behavior by the POSIX sh standard is quite small, and no equivalent to FUNCNAME is given; the closest thing is LINENO.

(If such a thing already existed in POSIX sh, why would bash have added its own rather than implementing the specification?)

Upvotes: 4

Related Questions