help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How to unset a local variable to expose the correspondin


From: Marco Silva
Subject: Re: [Help-bash] How to unset a local variable to expose the corresponding global variable in the function?
Date: Wed, 10 Aug 2016 11:23:05 -0300
User-agent: Sup/0.22.1

Excerpts from Peng Yu's message of 2016-08-08 07:39:09 -0500:
> On Mon, Aug 8, 2016 at 7:25 AM, Greg Wooledge <address@hidden> wrote:
> > On Sat, Aug 06, 2016 at 09:05:12PM -0500, Peng Yu wrote:
> >> Hi, The following example shows that once the variable tmp is set
> >> local in a function my_fun, the function can not access the
> >> corresponding global variable. Is there a way to still get the global
> >> variable as if the local variable has not been set at all?
> >
> > No.  Stop shadowing globals that you want to access with locals of the
> > same name.
> 
> That means that the author of the function needs to know what is the
> global environment in which the function is called, which makes the
> function not self-contained. This does not seem to be a good design
> practice.
> 

Shell is intended to be a command interpreter not programming language,
with complete features of modularization and packaging. The local
variable is mostly to protect the environment to not get polluted. The
intention of local is something like:

function  my_func() {
    echo $PATH
    local PATH="$PATH:~/tmp/bin"
    echo $PATH
}

my_func
echo $PATH

-- 
Marco Arthur @ (M)arco Creatives



reply via email to

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