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
local
keyword:ksh
has typically been capable of running POSIX commands, so it may be worthwhile to implement for that reason./bin/sh
to implementlocal
(and they don’t build withSHOPT_BASH
). Consequently, it isn’t possible to useksh
as/bin/sh
on a Debian or Ubuntu system.local
in order to run the testsuite and shell commands. Consequently, it isn’t possible to useksh
as 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 thatksh
fit 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, thenlocal
should 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
typeset
was introduced, thefunction
keyword was created precisely for supporting local variables. The AST group did not want to “contaminate” oldsh
style functions with different behaviour, so a new type of function was created.Interestingly, it works correctly if the function is defined using the
function
keyword.