【发布时间】:2018-01-12 23:46:47
【问题描述】:
我正在尝试将数组传递给 shell 脚本,如 this question 中所述。我写了一个小脚本,它只是用来接收数组的名称并打印出数组:
#!/bin/bash
echo "$1"
echo "${!1}"
arrayVar=("${!1}")
echo "${arrayVar[1]}"
我在运行脚本时声明了一个数组变量,如下所示:
array=(foo bar test) ./test.sh array[@]
输出:
|array[@] # the bars are only here to force the final blank line
|(foo bar test)
|
似乎array,实际上不是一个数组,而是简单的字符串(foo bar test)
即使我将脚本修改为直接按名称而不是通过位置参数间接回显array,我也会得到相同的结果。
#!/bin/bash
echo "$1"
arrayVar=("${!1}")
echo $arrayVar
echo "${arrayVar[1]}"
echo $array
echo "${array[1]}"
输出:
|array[@] # the bars are only here to force the final blank line
|(foo bar test)
|
|(foo bar test)
|
我只是做错了什么,还是 bash 不支持在命令之前分配数组?
【问题讨论】: