help-bash
[Top][All Lists]
Advanced

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

Local variables for nested function calls


From: eduardo-chibas
Subject: Local variables for nested function calls
Date: Wed, 28 Jul 2021 18:55:19 +0200

> Sent: Thursday, July 29, 2021 at 3:07 AM
> From: "Greg Wooledge" <greg@wooledge.org>
> To: help-bash@gnu.org
> Subject: Re: Local variables for nested function calls
>
> On Wed, Jul 28, 2021 at 04:53:01PM +0200, eduardo-chibas@caramail.com wrote:
> > I have a function `pregion` that is called from another function `plist`.
> > If `plist` sets the variable `fdir` using `local fdir=${dpath:-$PWD}`
> > but in `pregion` the variable `fdir` is used without being defined as
> > local, it then considers `fdir` to be assigned to the value set by the
> > parent function.
>
> Correct.  This is called "dynamic scope".
>
> > Everything gets very complicated.  I want to ask how to properly handle
> > things so that they do not come to bite you later on.
>
> However you feel is correct for your script.  You haven't actually
> stated a problem yet, or asked a question specific enough that we
> can give advice.
>
> > Furthermore, is it still acceptable to use `${parameter:-word}` and
> > `${parameter:=word}` these days ?  Rather than simply using
> > `[[ $fdir ]] || fdir=this` or `[[ -v fdir ]] || fdir=that`.
>
> You can use whatever you feel is best.  I dislike : "${foo:=bar}" but
> that's just me.
>
> local foo=${1:-default}   is far more readable.  I don't object to that.
>

Some object to this one,

local foo=${foo:-default}






reply via email to

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