[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Assignements to variables with integer attribute are expanded recur
From: |
Andreas Kähäri |
Subject: |
Re: [Assignements to variables with integer attribute are expanded recursively] |
Date: |
Wed, 8 Jan 2025 20:54:40 +0100 |
Je Wed, Jan 08, 2025 at 08:01:23PM +0100, joergboe--- via Bug reports for the
GNU Bourne Again SHell skribis:
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -O2 -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
> -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables
> -fstack-clash-protection -Werror=return-type -flto=auto -g -D_GNU_SOURCE
> -DRECYCLES_PIDS -Wall -g -Wuninitialized -Wextra -Wno-switch-enum
> -Wno-unused-variable -Wno-unused-parameter -Wno-parentheses
> -ftree-loop-linear -pipe -DBNC382214=0 -DIMPORT_FUNCTIONS_DEF=0
> -DDEFAULT_LOADABLE_BUILTINS_PATH='/usr/lib64/bash'
> uname output: Linux pluto 6.12.8-1-default #1 SMP PREEMPT_DYNAMIC Thu Jan 2
> 12:47:33 UTC 2025 (ab2dfe6) x86_64 x86_64 x86_64 GNU/Linux
> Machine Type: x86_64-suse-linux-gnu
>
> Bash Version: 5.2
> Patch Level: 37
> Release Status: release
>
> Description:
> [Assignments to variables with attribute 'integer' behave unexpectedly
> if the right side has not an integer value.
> It seems that the value is interpreted as a variable name and expansion
> is done recursively.
> This is not what I would expect.]
>
> Repeat-By:
> [
> > aa=123456
> > x=aa
> > declare -i i=x
> > echo $i
> 123456
> ]
>
>
>From the manual:
Shell variables are allowed as operands; parameter expansion
is performed before the expression is evaluated. Within an
expression, shell variables may also be referenced by name
without using the parameter expansion syntax. [...] The value of
a variable is evaluated as an arithmetic expression when it is
referenced, or when a variable which has been given the integer
attribute using declare -i is assigned a value.
The "The value of a variable is evaluated as an arithmetic expression"
effectively allows for recursive arithmetic expansion of the right-hand
side of an assignment to a variable with the integer attribute.
Recursive, that is, until the right-hand side is no longer a variable's
name, at which point the expansion stops and the value is assigned (or
an error is generated if the value is not a valid integer).
--
Andreas (Kusalananda) Kähäri
Uppsala, Sweden
.