On Fri, Jan 17, 2025 at 21:11:12 +0100, dv wrote:
> Description:
> # If the input string of a READ matches a variable in the script, REPLY is
> # set to the value that variable, not to the string.

> declare -i int

There's your problem: you're using the -i attribute on a variable without
understanding what it actually *does*.

When you put the -i attribute on a variable, every assignment to that
variable operates in an arithmetic context, as if you used the "let"
command.

And, when expressions are evaluated in an arithmetic context, any
expression that expands to a valid variable name is evaluated *as*
that variable, recursively.

Here's a very simple demonstration of how arithmetic expansion works
in bash:

hobbit:~$ a=b b=c c=d d=e e=42
hobbit:~$ x=$a; echo "$x"
b
hobbit:~$ let "x=$a"; echo "$x"
42

Without the "let", x=$a is a simple string assignment.  The value of
$a, whatever it is, is assigned to the variable x.  In this case, $a
contains the string "b", so x is assigned "b".

With "let", the expression "x=$a" is evaluated in an arithmetic context.
The value of $a is b, which is not a number, but it *is* a valid variable
name.  So bash recursively looks at the value of $b, and so on down the
line, until it gets to $e whose value is 42.  42 is a valid integer, so
that becomes the value assigned to x.

With "declare -i", you have exactly the same thing:

hobbit:~$ unset x
hobbit:~$ declare -i x
hobbit:~$ x=$a; echo "$x"
42

Because x has the -i attribute, the x=$a assignment happens in an
arithmetic context, and works just like 'let "x=$a"' did.

Reply via email to