bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

readonly inconsistency with arrays


From: Will Allan
Subject: readonly inconsistency with arrays
Date: Mon, 3 Jun 2024 21:54:14 +0000 (UTC)

I'm seeing what appears to be an inconsistency with using `readonly` on 
variables declared in a previous scope.

declare stringdeclare -i intdeclare -a array
init_vars () {  readonly string="foo"  readonly int=100  readonly array=(1 2)
  # Print the (hopefully) readonly variables  declare -p string int array}
init_vars
This prints the expected output (IMO):

declare -r string="foo"declare -ir int="100"declare -ar array=([0]="1" [1]="2")
Now if I attempt to do the same for local variables declared as such:

foo () {  local string  local -i int  local -a array
  init_vars}
foo
This prints different output for the indexed array only:

declare -r string="foo"declare -ir int="100"declare -ar array
Things get even more confusing if I declare an associative array, which 
readonly seems to think is now an indexed array, but maybe that's a different 
issue.
Thoughts?
TIA,

Will





reply via email to

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