help-bash
[Top][All Lists]
Advanced

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

Re: Local variables for nested function calls


From: Greg Wooledge
Subject: Re: Local variables for nested function calls
Date: Wed, 28 Jul 2021 11:07:06 -0400

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.



reply via email to

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