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 20:46:56 +0200

> Sent: Thursday, July 29, 2021 at 5:05 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 06:55:19PM +0200, eduardo-chibas@caramail.com wrote:
> > Some object to this one,
> >
> > local foo=${foo:-default}
>
> Well, yeah, you can see why.  What was 'foo' set to previously?  Why are
> you only declaring it local *now*, after it possibly already had some
> value?
>
> I don't understand the intent of this command, at least not without
> some context, so it's difficult for me to approve of it.
>
> What are you actually trying to do?

The problem started with a function `plist` that calls another function 
`region`.

----- function plist -----

ropts=("$@")

# read function arguments here
"-d")
  local -r dpath="$2" ; shift 2 ;;

local fdir=${dpath:-$PWD}

region "${ropts[@]}"

----- plist ends here -----


----- function region -----

# read function arguments here
 "-d")
    local fdir=$2 ; shift 2 ;;

: "${fdir:="${@:$#}"}"}

----- region ends here -----

When I was calling `plist` without the -d option, and subsequently calling
`region`, the value of `fdir` was being set to `$PWD`, rather than the last
argument passed to `region`.

This means that I have to declare my variables local without setting them
first.

But then I see no point of using parameter expansion such as `: 
"${fdir:="${@:$#}"}"}`
to set defaults because `fdir` would already be defined when I use the local 
declaration
within `region`.







reply via email to

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