Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Shell脚本:关联数组作为关联数组的值_Bash_Shell_Terminal - Fatal编程技术网

Bash Shell脚本:关联数组作为关联数组的值

Bash Shell脚本:关联数组作为关联数组的值,bash,shell,terminal,Bash,Shell,Terminal,尝试将关联数组指定为另一关联数组中的值: KEY_1="KEY_1" KEY_2="KEY_2" NAME="SOMENAME" declare -A MAIN_ARRAY #The main associated array declare -A CHILD_ARRAY # the child associated array CHILD_ARRAY[$KEY_1]="K1" CHILD_ARRAY[$KEY_

尝试将关联数组指定为另一关联数组中的值:

KEY_1="KEY_1"
KEY_2="KEY_2"
NAME="SOMENAME"

declare -A MAIN_ARRAY #The main associated array
declare -A CHILD_ARRAY # the child associated array

CHILD_ARRAY[$KEY_1]="K1"
CHILD_ARRAY[$KEY_2]="K2"

# assigning main child associated array HERE
MAIN_ARRAY[$NAME]=$CHILD_ARRAY

#reading back the sub associated array from the main associated array
CHILD_ARRAY=$MAIN_ARRAY[$NAME]
但是当我试图打印
键1
(即“K1”)的值时:

它抛出
超出数学递归限制:KEY_1
错误


这是正确的方法吗?

Bash和Zsh是不兼容的shell。删除其中一个标记。哦,wrt正在尝试将一个关联数组作为另一个关联数组中的值分配给另一个关联数组,这在Bash中是不可能的。数组中只能存储字符串。事实上,这几乎是Shell实际拥有的唯一数据类型。您可以将子数组的名称存储在主数组中,然后使用间接扩展之类的方法来获取子数组的内容,但如何实现这一点取决于您使用的shell。这是一个很好的解决方法。谢谢
echo "$CHILD_ARRAY[$KEY_1]"