bug-bash
[Top][All Lists]
Advanced

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

Re: [off-topic] ksh local vars [was: Unset array doesn't work]


From: Koichi Murase
Subject: Re: [off-topic] ksh local vars [was: Unset array doesn't work]
Date: Tue, 6 Mar 2018 00:09:54 +0900

2018-03-05 22:57 GMT+09:00 Greg Wooledge <wooledg@eeg.ccf.org>:
> On Sat, Mar 03, 2018 at 08:24:06PM +0900, Koichi Murase wrote:
>> - Note: ksh seems not support local variables. "typeset" in function
>> scopes defines a global variable, and "unset" removes the global
>> variable.
>
> "Real ksh" has two different kinds of functions -- ones declared with
> "foo()" and ones declared with "function foo".  They have different
> treatment of local variables, and the ksh man page does not clearly
> (at least to me) describe the difference.
>

Thank you for the information. I actually tested with the following
version of ksh:

    $ ksh --version
    version         sh (AT&T Research) 93u+ 2012-08-01

I tried again for ksh with the two kinds of functions. I considered
the following script:

    echo local typeset
    echo A: $(f() { typeset a=2; echo f:a=$a; }; f; echo g:a=$a)
    echo B: $(function f { typeset a=2; echo f:a=$a; }; f; echo g:a=$a)

    echo local-scope unset
    echo A: $(a=1; f() { typeset a=2; echo f:a=$a; unset a; echo
f:a=$a; }; f; echo g:a=$a)
    echo B: $(a=1; function f { typeset a=2; echo f:a=$a; unset a;
echo f:a=$a; }; f; echo g:a=$a)

    echo previous-scope unset
    echo A: $(a=1; u() { echo u:a=$a; unset a; }; f() { typeset a=2;
echo f:a=$a; u; echo f:a=$a; }; f; echo g:a=$a)
    echo B: $(a=1; function u { echo u:a=$a; unset a; }; function f {
typeset a=2; echo f:a=$a; u; echo f:a=$a; }; f; echo g:a=$a)

    echo local typeset without value
    echo A: $(a=set; f() { typeset a; echo ${a-unset}; }; echo $a; f; echo $a)
    echo B: $(a=set; function f { typeset a; echo ${a-unset}; }; echo
$a; f; echo $a)

The result was:

    local typeset
    A: f:a=2 g:a=2
    B: f:a=2 g:a=
    local-scope unset
    A: f:a=2 f:a= g:a=
    B: f:a=2 f:a= g:a=1
    previous-scope unset
    A: f:a=2 u:a=2 f:a= g:a=
    B: f:a=2 u:a=1 f:a=2 g:a=
    local typeset without value
    A: set set set
    B: set unset set

This means that `typeset' defines a global variable in the `foo()'
form of ksh functions, and defines a local variable in the `function
foo' form. Also I noticed that in the `function foo' form of
functions, if the variable is not defined in the local scope, the
variable name always refers to the global one even if the caller
function defines a local variable with the same name. The `unset'
builtin removes local variables in the `function foo' form of
functions if it is defined, and otherwise it removes global variables.
In the `function foo' form, when a local variable is defined without a
value, the variable has the `unset' state.

2018-03-05 23:49 GMT+09:00 Martijn Dekker <martijn@inlv.org>:
> In the current "real ksh", AT&T ksh93, things changed in radical and
> incompatible ways. Functions defined with "foo()" don't have local
> variables at all. Functions defined with "function foo" have a new model
> for local variables: static scoping. That is, if x calls y, then y does
> *not* have access to x's local variables. In static scoping, "unset" in
> a local scope can never influence the global scope. No other shell to
> date has emulated the ksh93 model.

Thank you very much for the additional information. The ksh that I
tested was actually ksh93, and your explanation helped me to
understand the behavior.

--
Koichi



reply via email to

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