Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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:关联数组的值可以是数组吗?什么';它的语法是什么?_Bash_Shell_Arrays - Fatal编程技术网

Bash:关联数组的值可以是数组吗?什么';它的语法是什么?

Bash:关联数组的值可以是数组吗?什么';它的语法是什么?,bash,shell,arrays,Bash,Shell,Arrays,我在bash文件中存储了一个关联数组,我希望所述关联数组的值是数组,但我甚至不知道这是否可行: 假设我的文件/tmp/conf.bash包含: declare-A关联=( [“foo”]=(“bar”“baz”) ) 当我尝试加载它时(使用source/tmp/conf.bash),我得到: borrajax@wharrgarbl:~#source/tmp/conf.bash bash:/tmp/conf.bash:第2行:意外标记“(”附近的语法错误 bash:/tmp/conf.bash:

我在bash文件中存储了一个关联数组,我希望所述关联数组的值是数组,但我甚至不知道这是否可行:

假设我的文件
/tmp/conf.bash
包含:

declare-A关联=(
[“foo”]=(“bar”“baz”)
)
当我尝试加载它时(使用
source/tmp/conf.bash
),我得到:

borrajax@wharrgarbl:~#source/tmp/conf.bash
bash:/tmp/conf.bash:第2行:意外标记“(”附近的语法错误
bash:/tmp/conf.bash:第2行:`[“foo”]=(“bar”“baz”)'
bash:/tmp/conf.bash:第3行:意外标记“)”附近的语法错误
bash:/tmp/conf.bash:line 3:`

但是,如果我制作文件:

declare -A ASSOCIATIVE=(
    ["foo"]="bar baz"
 )
它工作正常:

borrajax@wharrgarbl:~# source /tmp/conf.bash
borrajax@hwharrgarbl:~# for key in "${!ASSOCIATIVE[@]}"; do \
                        echo "key: $key; values: ${ASSOCIATIVE[$key]}"; \
                        done
key: foo; values: bar baz
有没有办法指定这些值是Bash脚本中的数组

一种可能是将关联数组的值设置为逗号分隔的字符串,然后将它们拆分为一个数组,但我认为可能有一种方法可以格式化bash文件,这样就不需要执行此步骤

我使用的是bash4.2.28


提前感谢。

否。数组中的值(无论是索引值还是关联值)只能是字符串。它有助于将
bash
数组看作第二级引用,而不是数据结构

args=("foo bar" "baz")
mycommand "${args[@]}"
将两个参数(而不是三个)传递给
mycommand
。在这种情况下,不需要嵌套,因为命令只能将字符串而不是数组作为参数