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: Chet Ramey
Subject: Re: Local variable can not unset within shell functions
Date: Thu, 11 Jul 2024 16:01:20 -0400
User-agent: Mozilla Thunderbird

On 7/11/24 9:42 AM, Dr. Werner Fink wrote:
Hi,

I've a report that with later bash the following which works in bash-4.2

  x () {
   local x=y
   declare -p x
   echo $x
   unset x
   declare -p x
   echo $x
  }

with

  linux-40cm:~ # x () {
  >   local x=y
  >   declare -p x
  >   echo $x
  >   unset x
  >   declare -p x
  >   echo $x
  >  }
  linux-40cm:~ # x
  declare -- x="y"
  y
  -bash: declare: x: not found
but with bash-5.X the reporter sees (and complains)

  sl15sp5:~ # x () {
  >   local x=y
  >   declare -p x
  >   echo $x
  >   unset x
  >   declare -p x
  >   echo $x
  >  }
  sl15sp5:~ # x
  declare -- x="y"
  y
  declare -- x

The variable is unset: it has attributes (local) but has not been assigned
a value.

The idea behind preserving the local attribute is that the variable will
continue to be reported as unset while in the function, but assigning it
a new value will preserve it as a local variable. This is how bash has
behaved since 1995 (bash-2.0).

The bug in bash-4.2 was that it didn't tell you which instance of a
variable the shell would modify if it were subsequently assigned a value.
The local variable still exists, is unset, and would be used by expansions
and assignments, but `declare' would not tell you that.

This would have been clearer to see (and more misleading) if the variable
x also had a value at the global scope.

A discussion that prompted the change starts at

https://lists.gnu.org/archive/html/bug-bash/2013-11/msg00000.html

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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