小编Den*_*nis的帖子

Bash 4.4 本地只读数组变量范围:bug?

使用 bash 4.4.20(1) 运行以下脚本时失败

#!/bin/bash
bar() {
  local args=("y")
}

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

foo
Run Code Online (Sandbox Code Playgroud)

有错误,但使用 bash 4.2.46(2) 运行时成功,这在阅读24.2line 3: args: readonly variable后有意义。局部变量。

以下带有非数组变量的脚本运行时没有任何问题:

#!/bin/bash
bar() {
  local args="y"
}

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

foo
Run Code Online (Sandbox Code Playgroud)

我找不到任何可以解释 bash 4.2.46(2) 和 bash 4.4.20(1) 之间差异的更改。

问:这是 bash 4.4.20(1) 中的错误吗?如果这是预期的行为那么为什么第二个脚本不会失败?

bash shell-script bash-array

7
推荐指数
1
解决办法
358
查看次数

标签 统计

bash ×1

bash-array ×1

shell-script ×1