(I wrote):
>> $ unset name zebra
>> $ name=zebra
>> $ zebra=99
>> $ echo $((name))
>> 99
>> What is the explanation for the third result? Is it documented?
(Causing Greg to write):
>When a variable name appears in an arithmetic context, it is expanded
>*recursively* until bash either gets an integer value, and empty string
>(which is treated as 0), or a string which is neither an integer value
>nor a possible variable name (which is treated as an error).
>hobbit:~$ a='b*c+d' b=2 c=3 d=4; echo $((a))
>10
>This is also a potential code injection vector:
>hobbit:~$ a='x[$(date >&2)0]'; echo $((a))
>Wed Jul 2 10:11:28 EDT 2025
>0
>Isn't bash wonderful? :-(
Wow.
Pardon me for saying this, but this sounds insane. Who dreamed this up?
I mean, if I really thought about it, I could probably come up with a situation
where
this would be useful, but it seems like most of the time, it would just be:
a) Totally unexpected
and
b) Dangerous - both from a "code injection" (i.e., security) POV, but also
from a
"Just looking at the code, there's no way to really know what it will
do" POV.
And, finally, again, where can I read about this wonderful "feature"? I
couldn't find
it in "man bash".