a='x[$(date >&2)0]=1'; echo $((a))
Thu Jul 3 04:51:16 CEST 2025
1
On Wed, Jul 2, 2025, 4:12 PM Greg Wooledge <greg@wooledge.org> wrote:
> On Wed, Jul 02, 2025 at 06:44:17 -0600, Stan Marsh wrote:
> > $ unset name zebra
> > $ name=zebra
> > $ zebra=99
> > $ echo $((name))
> > 99
>
> > What is the explanation for the third result? Is it documented?
>
> 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? :-(
>
>