ast: local keyword is not recognized in a function
Rerpoducer script:
test_func() {
local stat=$(< /proc/self/stat)
echo $stat
}
test_func
Running this script gives this error :
local: local can only be used in a function
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (8 by maintainers)
I’ll point out a few things about the
localkeyword:kshhas typically been capable of running POSIX commands, so it may be worthwhile to implement for that reason./bin/shto implementlocal(and they don’t build withSHOPT_BASH). Consequently, it isn’t possible to usekshas/bin/shon a Debian or Ubuntu system.localin order to run the testsuite and shell commands. Consequently, it isn’t possible to usekshas the shell for Git.local:bash,dash,mksh,pdksh(and derivatives, like OpenBSD’sksh), andzsh.The first three would necessitate an implementation outside of
SHOPT_BASH. Now, nothing requires thatkshfit these niches; it may be that folks prefer to preserve historical behavior over addinglocal, and that’s fine. But if there’s a desire to fill these use cases, thenlocalshould probably be implemented as a standard feature.@hlangeveld This is consistent with my understanding as well.
func()-style functions behave assh,function func-style functions support things likelocal.IIRC The AST group subscribed strongly to the idea of one interface, one name, one behaviour. When introducing new behaviour they would go out of their way to create a new interface with a new name.
From what I remember when
typesetwas introduced, thefunctionkeyword was created precisely for supporting local variables. The AST group did not want to “contaminate” oldshstyle functions with different behaviour, so a new type of function was created.Interestingly, it works correctly if the function is defined using the
functionkeyword.