Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: msys Compiler: gcc Compilation CFLAGS: -march=nocona -msahf -mtune=generic -O2 -pipe -D_STATIC_BUILD uname output: MINGW64_NT-10.0-19045 Zack2021HPPavilion 3.5.3.x86_64 2024-06-03 06:22 UTC x86_64 Msys Machine Type: x86_64-pc-msys
Bash Version: 5.2 Patch Level: 26 Release Status: release Description: It just occurred to me that I could take advantage of arithmetic evaluation being performed when a variable with the -i integer attribute is assigned a value to create a visual distinction between working with regular variables and variables representing integers, i.e.: local this_var="words words words" local var[index]="${this_var}" local -r -i BIT_FLAG=2#0100 local -i value=BIT_FLAG After doing this, var[index] will be set to "words words words" and value will be set to 4. Going a step further and passing integer arguments to functions as the name of the variable, rather than passing its value with a parameter expansion of that variable, we run into a problem. I would expect func1 () and func2 () below to be equivalent. However, we see that when setting a local variable with the integer attribute, using the name of a variable at calling scope with the same name, the local variable is always assigned the value 0, if I'm not using an arithmetic expansion. I wondered if the call to 'local' was already referencing the local variable that it was in the process of declaring, which I imagine would've been unset at the time. 'set -o nounset' didn't cause it to error out, though. Of course 'declare -i other=other' is a no-op, but it doesn't break. Repeat-By: $ cat integer-scope #!/usr/bin/env bash set -o nounset main () { printf 'foo :\n' declare -i foo=1 func1 foo func2 foo printf 'bar :\n' declare -i bar=2 func1 bar func2 bar printf '"${baz}" :\n' declare -i baz=3 func1 "${baz}" func2 "${baz}" printf 'other :\n' declare -i other=4 declare -i other=other declare -p other } func1 () { declare -i bar="${1}" declare -p bar } func2 () { declare -i bar="$(( ${1} ))" declare -p bar } main $ ./integer-scope foo : declare -i bar="1" declare -i bar="1" bar : declare -i bar="0" declare -i bar="2" "${baz}" : declare -i bar="3" declare -i bar="3" other : declare -i other="4"