bug-bash
[Top][All Lists]
Advanced

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

Re: Defining variable as local -r defines variable outside the function


From: Koichi Murase
Subject: Re: Defining variable as local -r defines variable outside the function in some cases
Date: Tue, 14 Feb 2023 01:17:32 +0900

2023年2月14日(火) 0:39 Voldemar Lazarev via Bug reports for the GNU Bourne
Again SHell <bug-bash@gnu.org>:
> Description:
>     When a function has a variable (e.g. "X") defined as local and read-only 
> ("local -r") and the function is called with variable of same name ("X") 
> specified before function name (see the "Repeat-By" section), the variable 
> becomes defined outside the function.

This is a consequence of the combination of two different rules.

1) When the tempenv (the temporary environment, i.e., the environment
variables temporarily created for commands of the form `tempenv=val
cmd') was marked as "export" or "readonly" by `export', `readonly',
`declare -x', `declare -r', etc., the tempenv gets `att_propagate'
attribute and it propagates to the outer context.

$ a=0
$ a=1 declare a
$ echo $a
0
$ a=1 export a
$ echo $a
1

The same thing happens also when you replace `export' in the above
example with `readonly', `declare -x', `declare -r', etc.

2) The tempenv is created in the function scope for `tempenv=var
func'. You can observe it with the following commands:

$ func() { local a; declare -p a; }
$ func
declare -- a
$ a=123 func
declare -x a="123"

I'm not sure if these rules are documented, but I knew these rules
from the codebase. I guess these are somewhat intentional designs, but
I'm not sure about the background.

--
Koichi



reply via email to

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