helpThe GNU Bourne-Again SHell - Support: sr #110380, unset do not work with local in...


sr #110380: unset do not work with local in some cases

Submitter:  None
Submitted:  Sat 21 Nov 2020 11:38:03 AM UTC
Category:  None Priority:  5 - Normal
Severity:  4 - Important Status:  Invalid
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


Mon 21 Dec 2020 03:38:38 PM UTC, comment #2: 

This is how dynamic variable scoping works in bash, and has always worked. The `localvar_unset' shopt is to provide alternate behavior for those applications that need it, but does not change the default behavior. The default remains as it always has for backwards compatibility.

Chet Ramey <chet>
Project Administrator
Sat 21 Nov 2020 12:24:36 PM UTC, comment #1: 

I've found 'localvar_unset' shopt in bash 5.0.

So the only one remaining problem is that this shopt must be enabled by default. There is no reason to keep backward compability, because documented behavior of unset always was remove variable, not changing any scope. And after calling 'unset' variable must be removed - it will be expected always by all future users.

Sat 21 Nov 2020 11:38:03 AM UTC, original submission:  

Example (bash 4.4, debian buster):

function func2() {
    echo "func2 1: '${a-UNSET}'"
    unset a
    echo "func2 2: '${a-UNSET}'"

function func1() {
    local a
    echo "func1 1: '${a-UNSET}'"
    echo "func1 2: '${a-UNSET}'"

echo "global 1: '${a-UNSET}'"
echo "global 2: '${a-UNSET}'"

echo "global 3: '${a-UNSET}'"
echo "global 4: '${a-UNSET}'"

What I expect:

global 1: 'global'
func2 1: 'global'
func2 2: 'UNSET'
global 2: 'UNSET'
global 3: 'global'
func1 1: 'local'
func2 1: 'local'
func2 2: 'UNSET'
func1 2: 'UNSET'
global 4: 'global'

What I see:

global 1: 'global'
func2 1: 'global'
func2 2: 'UNSET'
global 2: 'UNSET'
global 3: 'global'
func1 1: 'local'
func2 1: 'local'
func2 2: 'global' (!)
func1 2: 'global' (!!)
global 4: 'trash' (!!!)

So in some cases:
1. 'unset' do not do unset (!)
2. 'unset' breaks local variables of top functions (!!)
3. And then top function breaks global scope (!!!)

All problems are critical, because if 'unset' was success - variable MUST always be unsetted on the one hand. On the other - if you declare variable as local in one function (see func1), you  expect that you can't break global variable with the same name. In all cases - 'unset' MUST always unset just nearest local variable and never change it's scope. Because otherwise behavior of 'unset' is undefined (it shouldn't matter if you call func2 from global scope or from other function - the result must be the same)



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.





    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-12-21 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.9