Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 108:29:04 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,683 |
Bash has a "nameref" attribute on variables,
that basically makes them like
"pointers" in languages like C. That is, the nameref variable has a value, which is the name of another variable, and any/all references to the
nameref are, in fact, references to the other variable (with 2 exceptions
as discussed below). So, it works like this:
# Create a nameref variable and give it the value "foo"
declare -n MyNameRef=foo
foo=bar
echo $MyNameRef
(prints bar)
MyNameRef="Not bar"
echo $foo
(prints Not bar)
Now, the docs present this feature as being primarily useful with
functions. That is, something like:
foo() {
local -n MyNameRef=$1
# Function manipulates MyNameRef, but is, in fact, manipulating
# the variable whose name was passed as an arg to the function.
}
So, if we call foo as: foo bar
then references to MyNameRef inside foo() are, in fact, references to the global variable "bar".
This is all well and good, but it leaves open the question of "How do I assign a value to the nameref itself, given that any reference to the
nameref gets converted to a reference to whatever the nameref points to?"
The answer, as far as I can tell, is that there are exactly 2 exceptions to the "always dereference" rule (for setting the value):
1) Use "declare" (or one of its synonyms, e.g., "local") to do the
assignment. This works, but is somewhat unappealing, since you may
end up declaring a variable more than once (which seems intuitively
wrong).
2) Use a "for" loop, like this:
for MyNameRef in one two three; do echo $MyNameRef; done
This magically assigns MyNameRef the values one, two, and three and
displays the values of those variables. Note that is different in
effect from the seemingly equivalent:
MyNameRef=one; echo $MyNameRef
MyNameRef=two; echo $MyNameRef
MyNameRef=three; echo $MyNameRef
Note: There is a syntax for reading the value of the nameref, but this
cannot be used to set the value. The syntax is: echo ${!MyNameRef}
In conclusion, my "reason for posting" is to confirm that I've got it right that there are only 2 ways to set the value. Am I missing anything?
On 08.12.2024 13:51, Kenny McCormack wrote:
Bash has a "nameref" attribute on variables,
Is that a newer feature of bash? (My version doesn't support -n with
'local' or with 'declare'.) My response is based on the ksh feature.
The underlying ideas of the feature might be reflected in both shells
in similar ways.
In article <vj48k5$3r1bq$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 08.12.2024 13:51, Kenny McCormack wrote:
Bash has a "nameref" attribute on variables,
Is that a newer feature of bash? (My version doesn't support -n with
'local' or with 'declare'.) My response is based on the ksh feature.
The underlying ideas of the feature might be reflected in both shells
in similar ways.
I think I read somewhere that it came in at bash 4.3 (c. 2014/2015) - which is rather old now. So, I wouldn't call it "new".
On 08.12.2024 15:41, Kenny McCormack wrote:
In article <vj48k5$3r1bq$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 08.12.2024 13:51, Kenny McCormack wrote:
Bash has a "nameref" attribute on variables,
Is that a newer feature of bash? (My version doesn't support -n with
'local' or with 'declare'.) My response is based on the ksh feature.
The underlying ideas of the feature might be reflected in both shells
in similar ways.
I think I read somewhere that it came in at bash 4.3 (c. 2014/2015) - which >> is rather old now. So, I wouldn't call it "new".
Yeah - "newer" is a very subjective and context specific term.
It's my [deliberately frozen] standard box I use regularly that
is still running bash 2.4; so some, erm.., "newer" features of
bash I cannot test [on that system] (without manual changes).
I see a downloaded "bash 5.0" package lying around on the file
system (not installed), but that's it. :-)
This is all well and good, but it leaves open the question of "How do I
assign a value to the nameref itself, given that any reference to the
nameref gets converted to a reference to whatever the nameref points to?"
If used in functions - where the actual function parameter from the
call cannot be changed from within the function ex post - it makes
sense to not "overwrite" the calling parameter.
[...]
I'm a fan of "retro" software, too, and I'm skeptical of the sort of people you often see who act as if if you aren't running the absolute latest and greatest, then your input is useless.
[...] I'm wondering "how alpha is alpha?"