Sample Header Ad - 728x90

Bash 4.4 local readonly array variable scoping: bug?

7 votes
1 answer
424 views
The following script fails when run with bash 4.4.20(1)
#!/bin/bash
bar() {
  local args=("y")
}

foo() {
  local -r args=("x")
  bar
}

foo
with error line 3: args: readonly variable but succeeds when run with bash 4.2.46(2), which makes sense after reading [24.2. Local Variables](https://tldp.org/LDP/abs/html/localvar.html) . The following script with non-array variables runs without any issues:
#!/bin/bash
bar() {
  local args="y"
}

foo() {
  local -r args="x"
  bar
}

foo
I could not find any [changes](https://git.savannah.gnu.org/cgit/bash.git/tree/CHANGES) that explain the difference between bash 4.2.46(2) and bash 4.4.20(1). Q: is this a bug in bash 4.4.20(1)? if this is expected behavior then why does the second script not fail?
Asked by Dennis (73 rep)
Feb 22, 2023, 10:52 AM
Last activity: Feb 22, 2023, 02:02 PM