On Fri, Jan 8, 2010 at 10:18 PM, J.C. Roberts <list-...@designtools.org> wrote: > I've been looking for standards on "correct" variable scope in ksh but > the IEEE Std 1003.1-2008 stuff on the Open Group site doesn't mention > any rules? > http://www.opengroup.org/onlinepubs/9699919799/utilities/contents.html
The key section is 2.12: http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12 To selectively quote: ---- A shell execution environment consists of the following: ... * Shell parameters that are set by variable assignment (see the set special built-in) or from the System Interfaces volume of POSIX.1-2008 environment inherited by the shell when it begins (see the export special built-in) ... A subshell environment shall be created as a duplicate of the shell environment, except that signal traps set by that shell environment shall be set to the default values. Changes made to the subshell environment shall not affect the shell environment. ... Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or all commands in a pipeline may be executed in the current environment. All other commands shall be executed in the current shell environment. ---- > I found an interesting behavior in our ksh (pdksh) implementation, and > I'd like to know if the behavior is considered correct? Yes, it is. > When you append to a variable within a 'for' loop, the changes are > exist after the loop ends, but if you do the same within a 'while' > loop, the changes are lost? It has nothing to do with 'for' vs 'while' and everything to do with your using 'for' as a single-command and 'while' in a multi-command pipeline. If you prefix the 'for' command with "echo foo |" then you'll see the same behavior with it. > I realize the IEEE/POSIX standard was based on ksh93, and our pdksh is > only partially compliant with ksh93, but I'm wondering if the "correct" > behavior for variable scope is formally standardized and/or documented > anywhere? See above. Philip Guenther