help-bash
[Top][All Lists]
Advanced

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

Re: Why does this do what it does: $((name)) ?


From: Stan Marsh
Subject: Re: Why does this do what it does: $((name)) ?
Date: Thu, 03 Jul 2025 07:26:03 -0600

(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".




reply via email to

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