向函数传递多个参数,一个是数组,另一个是带空格的变量

Pass multiple parameters to function, one being an array, another being a variable with spaces

我需要向 bash 中的函数发送多个参数。参数将是具有 space 的变量,或数组

问题:

尝试在函数中调用我的输入参数数组时,我一直收到 bad substitution。我还 bash 没有正确处理第一个参数,只显示 space。 如何将这两种类型的参数传递给函数并在函数中正确使用它们?

这是我的代码:

#!/bin/bash

arr_conf=()

output(){
    echo 
    for i in "${2[@]}";do
        echo $i
    done
}

arr_conf=(
"a=1"
"b=2"
"c=3"
)

name="Mr. Test"
output $name "${arr_conf[@]}"

这是输出:

$ ./test.sh
Mr.
./test.sh: line 7: ${[@]}: bad substitution

双引号变量。使用 shift 从位置参数中删除第一个参数。

#! /bin/bash
output(){
    echo ""

    shift
    for i in "$@" ; do
        echo "$i"
    done
}

arr_conf=( "a=1" "b=2" "c=3" )
name="Mr. Test"
output "$name" "${arr_conf[@]}"