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: Greg Wooledge
Subject: Re: Why does this do what it does: $((name)) ?
Date: Wed, 2 Jul 2025 10:12:05 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

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? :-(



reply via email to

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