bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Local variable names clash with global read-only variable names.


From: Dale R. Worley
Subject: Re: Local variable names clash with global read-only variable names.
Date: Thu, 30 Apr 2020 22:43:53 -0400

>>>     f() { local x=a; }
>>>     declare -r x
>>>     f  # bash: local: x: readonly variable
>>>
>>>   This^^^ should not fail; it hinders reusability of shell functions and 
>>> makes
>>>   them context-dependent.

It's "natural" to think that a variable that is local to a function
should be independent of however it is declared globally.  The problem
is that bash local variables aren't lexically scoped -- when you enter
the function, the variable acquires the new value, but any function that
is called by this function sees the new value, even if that function
wasn't declared inside the function that declared the variable local.
So if a local declaration could override a readonly declaration, the new
value could be seen by code unrelated the function that did the
override.

This is a common issue in language design.  The Perl language originally
only had "local" declarations that behaved the same way as bash local
declarations.  But the above behavior got to be so much of a problem for
large programs that Perl added a separate lexically-scoped local
declaration.

Dale



reply via email to

[Prev in Thread] Current Thread [Next in Thread]