2014-12-10 06:33:00 +0800, konsolebox: [...] > > Not sure what you mean by that last sentence, but all of dash, > > yash, mksh, pdksh, AT&T ksh, posh, zsh (so virtually all modern > > Bourne-like shells) do support local scopes each with variations > > of behaviour and syntax. > > I know ksh, pdksh and zsh supports local but I didn't know that dash > does it too - or maybe I forgot. I always remember that it's a pure > POSIX shell but apparently it's not.
dash (and posh) follow (or at least used to) another standard, the Debian policy which is more or less POSIX with a few additions (from XSI, UP and its owns). In particular, it mandates a "local" builtin. So sh scripts that use "local" are guaranteed to work in Debian. However note that behaviour for "local foo=bar" is unspecified there, so sh scripts for Debian should use "local foo; foo=bar" (even those POSIX specifies "export foo=bar" (though not clearly enough)). Last time I checked, the Debian spec didn't specify the exact variable scoping (for instance whether dynamic รก la ksh88/bash/dash/yash/pdksh or lexical a la ksh93). There have been discussions on specifying local scopes in the past, but it's hard to get a consensus. See the discussion at http://thread.gmane.org/gmane.comp.standards.posix.austin.general/8371/focus=8377 David Korn mentions that local scoping a la ksh was originally rejected by POSIX because ksh88 originally implemented "dynamic scoping" (like most shells now do, but most other languages don't). ksh changed to lexical scoping in ksh93 (probably partly to address that POSIX /requirement/) and now everyone else wants dynamic scoping because that's how most shells have implemented it in the interval. > And sorry I actually meant > "some other". Consider a more conservative shell like heirloom-sh. [...] Note that that's the Bourne shell, not a POSIX sh. It's ironic that local scoping is now implemented in virtually all POSIX shells but not specified by POSIX. -- Stephane