如何将数组传递给函数,为什么这不起作用?
其他问题中的解决方案对我不起作用。
作为记录,我不需要复制数组,所以我不介意传递引用。我想要做的就是循环它。

$ ar=(a b c)
$ function test() { echo ${1[@]}; }
$ echo ${ar[@]}
a b c
$ test $ar
bash: ${1[@]}: bad substitution
$ test ${ar[@]}
bash: ${1[@]}: bad substitution

最佳答案

#!/bin/bash
ar=( a b c )
test() {
    local ref=$1[@]
    echo ${!ref}
}

test ar

关于arrays - 如何将数组传递给 bash 函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8082947/

10-15 17:59