bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing variables by reference conflicts with local


From: Chet Ramey
Subject: Re: Passing variables by reference conflicts with local
Date: Sat, 28 Apr 2012 22:03:46 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:11.0) Gecko/20120327 Thunderbird/11.0.1

On 4/26/12 8:11 AM, Greg Wooledge wrote:

> Bash uses "dynamic scoping", which is a somewhat controversial choice, and
> has led to a whole lot of confusion in the past.  Most other shells use
> static scoping.

I really wish people would stop saying "most other shells," unless by that
they mean "ksh93".  Consider this script (it's necessary to use `function'
so that ksh93 turns on static scoping) or a slight variant that uses the
Posix declaration syntax and `local' instead of `typeset' (necessary for
dash and other ash descendents):

var=global

function f1
{
        typeset var=f1-local
        f2
}

function f2
{
        echo f2: $var
}

echo main: $var
f1
echo main2: $var

The following shells print

main: global
f2: f1-local
main2: global

indicating dynamic scoping: bash, zsh, dash, mksh, FreeBSD sh, posh, and
the venerable 4.4 BSD sh.

ksh93 is the only one that prints

main: global
f2: global
main2: global

Unless you export var in f1, in which case you get one level of dynamic
scoping.

> POSIX has nothing to say on the matter; "local" is not
> defined in POSIX, and POSIX does not even require functions to implement
> local variables at all (let alone dictate what kind of scoping to use).

Posix did specify `local' once, in draft 9 of what became the Posix.2
1992 specification.  That specification informed the 4.4 BSD sh
implementation, which used dynamic scoping, since the guy at Berkeley
who prepared sh for 4.4 BSD wrote most of d9 (Marc Teitelbaum).  While I
no longer have my copy of d9 (more's the pity), I believe that it
specified dynamic scoping for its local variables.  This stuff all
predates ksh93.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]