[Top][All Lists]

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

Variable scoping

From: Matthew Markopoulos
Subject: Variable scoping
Date: Wed, 19 Feb 2003 14:19:59 +0700


I have a question about scoping in bash vs ksh which I've pursued in other
forums but without much success.

According to the bash manual, if I use typeset to declare a variable in a
function, the scope of that variable will be restricted to the function and
its children (because typeset acts like local). This works as I expect:

$ cat scopetest
function outer {
    typeset X=0
function inner {
    echo $X
$ ./scopetest

Inner apparently inherits the scope of outer when called from that function,
but inherits global lexical scope when called directly.

Moving on to ksh93. According the AT&T ksh93 manual, if I use typeset to
declare a variable in a function defined with the 'function name {}' syntax,
the scope of that variable includes the function and all functions it calls.
This behaviour seems to me to parallel that of typeset in bash functions.
However, if I rerun the same script under ksh93, it prints 15, then 15.
Apparently, inner does not see the scope of its caller when called from
outer. To get a similar result to bash, I must use 'X=$X inner' in ksh93.

Is this a documented difference in scoping between bash and ksh? Or does the
typeset command work differently in bash vs ksh, despite what the respective
manuals say? Or is my understanding of the manuals incorrect?

Any help would be much appreciated.


Matt M

reply via email to

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