help-bash
[Top][All Lists]
Advanced

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

Re: Understanding internal variable scope


From: Chet Ramey
Subject: Re: Understanding internal variable scope
Date: Wed, 1 Jun 2022 15:06:47 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.9.1

On 5/28/22 10:36 AM, Robert E. Griffith wrote:

> 1) I find that when my loadable builtin is called from a bash function,
> sometimes the following code works to create a local variable in the
> calling function and sometimes it does not. If I do "local myVariableName"
> before calling the builtin, it always receives hte value.
> 
>    bind_variable_value(make_local_variable("myVariableName",0),
> "SomeValue..., 0)

There would have to be a more reproducible example to say why.

> 2) There seems to be a difference between sourcing a script at global scope
> as opposed to sourcing it from inside a bash function but I cant quite pin
> it down. Sometimes variables declared in the global scope "declare <myvar>"
> of a sourced script do not exist after sourcing the script but functions
> from the script always do.

Variables declared using `declare' when bash is executing inside a
function, whether it's from a sourced script or not, should always be
local, with the usual scoping and `shadowing' rules. There are no such
things as local functions.

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



reply via email to

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