Separate namespaces for functions and variables in POSIX shells

A guarantee:

2.9.5 Function Definition Command

A function is a user-defined name that is used as a simple command to call a compound command with new positional parameters. A function is defined with a "function definition command". [...]

The function is named fname; the application shall ensure that it is a name (see XBD Name) and that it is not the name of a special built-in utility. An implementation may allow other characters in a function name as an extension. The implementation shall maintain separate name spaces for functions and variables.


Variables and functions reside in different namespaces in dash and this is also specified by POSIX:

The implementation shall maintain separate name spaces for functions and variables.

In addition to that, variables have global scope, by default. Some shells (e.g. bash, ksh and zsh) provide the local keyword to declare variables in a function with local scope only.

So, yes, the behavior you are seeing is guaranteed by POSIX.

POSIX hasn't standardized local, yet:

The description of functions in an early proposal was based on the notion that functions should behave like miniature shell scripts; that is, except for sharing variables, most elements of an execution environment should behave as if they were a new execution environment, [..]

[..] Local variables within a function were considered and included in another early proposal (controlled by the special built-in local), but were removed because they do not fit the simple model developed for functions and because there was some opposition to adding yet another new special built-in that was not part of historical practice. Implementations should reserve the identifier local (as well as typeset, as used in the KornShell) in case this local variable mechanism is adopted in a future version of this standard.

(emphasis mine)

Tags:

Shell

Posix

Dash