从函数的输出创建Bash关联数组:为什么“declare-Afoo=$(bar)”有效,但“declare-Afoo;foo=$(bar)”无效?

考虑以下 bash 片段。在其中,我以不同的方式创建两个关联数组,然后打印其中一个条目。

场景 1.在一个语句中声明和赋值。按预期工作:

make_person() { echo '([firstName]=Bob [lastName]=Black)'; }
declare -A person1=$(make_person)
echo "${person1[firstName]}"

输出:

Bob

场景 2.在两条不同的行上声明和分配。无输出:

declare -A person2
person2=$(make_person)
echo "${person2[firstName]}"

输出:

为什么方案 1 可以成功打印[firstName]条目而方案 2没有?两个场景都定义了和预期的行为吗?

回答

因为declare 重新解释和解析参数以检测( )变量赋值,然后进行数组赋值。而在正常分配var=anything始终是一个正常的分配,在var=(anything)所述( )检测到解析时的表达(当bash读取的行/输入来执行)。declare更像是eval,它重新评估输入。

场景 1 成功打印 [firstName] 条目而场景 2 没有打印的 bash 特定原因是什么?

“bash 特定原因”是我猜它是这样编程的。原因也是为了与 POSIX 兼容,因此var=$(anything)应该会像 POSIX 一样产生一个正常的变量。

是否定义了两种场景

“定义”有点太多了,我在 bash 文档中没有看到关于这个特定语法的太多内容,但是

和预期的行为?

是的,这是按预期工作的。

  • Exactly. The shell expands `$(make_person)` and as a result `declare` receives a legal compound assignment in scenario 1. In scenario 2, the assignment is considered scalar since the equals sign is not followed by an open paren, and the expansion is performed afterwards.

以上是从函数的输出创建Bash关联数组:为什么“declare-Afoo=$(bar)”有效,但“declare-Afoo;foo=$(bar)”无效?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>