5 ms·
Without disagreeing with the essence of what you said: > Assignment to hard-coded global variable ... Because Bash is dynamically-scoped, it's not necessarily
by LukeShu 7y ago
Without disagreeing with the essence of what you said:
> Assignment to hard-coded global variable ...
Because Bash is dynamically-scoped, it's not necessarily global; it could be scoped to the calling function. For example:
setit() {
myvar=bar
}
myfunc() {
local myvar
setit
echo myfunc $myvar # will print: myfunc bar
}
myvar=foo
myfunc # will print: myfunc bar
echo global $myvar # will print: global foo
- kazinator 7y agoMost shell programmers do not know what "dynamic scope" is, lacking a Lisp background. Those familiar with lexical languages like C or Java will naively expect "local" to be lexical. Unlike in dynamically scoped Lisps, in shell programming there isn't any widely recognized and applied naming convention to avoid accidental name capture due to dynamic scope (binding a variable, not knowing that a function which is then used accesses that as a global). POSIX has no "local", and so makes no recommendation in this regard, nor sets any naming precedent for users programs to follow. It is more concerned with the separation between system variables (named with ALL_CAPS) and user variables (not so named). Put these two together and you have a recipe for bugs.