bug-bash
[Top][All Lists]
Advanced

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

Re: Local variable can not unset within shell functions


From: Greg Wooledge
Subject: Re: Local variable can not unset within shell functions
Date: Thu, 11 Jul 2024 15:54:47 -0400

On Thu, Jul 11, 2024 at 15:39:41 -0400, Lawrence Velázquez wrote:
> I won't speculate about the issue, but your subject goes too far.
> The variable really is unset here:
> 
>       % cat /tmp/x.bash
>       x() {
>               local x=y
>               declare -p x
>               echo "x is ${x-unset}"
>               unset x
>               declare -p x
>               echo "x is ${x-unset}"
>       }
> 
>       x
>       % bash /tmp/x.bash
>       declare -- x="y"
>       x is y
>       declare -- x
>       x is unset

It looks like newer versions of bash retain *some* memory of the unset
local variable's name, but not its flags or prior contents.

hobbit:~$ f() { local -i x=0; declare -p x; unset x; declare -p x; }
hobbit:~$ f
declare -i x="0"
declare -- x

Lawrence is spot on with the semantics, though.  The unset variable
behaves exactly like a variable that was never set.

hobbit:~$ g() { local -i x=0; unset x; echo "plus:${x+plus} minus:${x-minus}";}
hobbit:~$ g
plus: minus:minus

So the question is why Werner's associate cares enough about the output
of declare -p to call this a bug, rather than a simple change of internal
implementation.

Is there some actual semantic difference in behavior between bash versions
that we need to be concerned about here?



reply via email to

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