如何将数组作为参数传递给 Bash 中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16461656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:41:13  来源:igfitidea点击:

How to pass array as an argument to a function in Bash

arraysbashshell

提问by Red Lv

As we know, in bash programming the way to pass arguments is $1, ..., $N. However, I found it not easy to pass an array as an argument to a function which receives more than one argument. Here is one example:

我们知道,在 bash 编程中,传递参数的方式是$1, ..., $N。但是,我发现将数组作为参数传递给接收多个参数的函数并不容易。这是一个例子:

f(){
 x=()
 y=

 for i in "${x[@]}"
 do
  echo $i
 done
 ....
}

a=("jfaldsj jflajds" "LAST")
b=NOEFLDJF

f "${a[@]}" $b
f "${a[*]}" $b

As described, function freceives two arguments: the first is assigned to xwhich is an array, the second to y.

如上所述,函数f接收两个参数:第一个被分配给x数组,第二个被分配给y

fcan be called in two ways. The first way use the "${a[@]}"as the first argument, and the result is:

f可以通过两种方式调用。第一种方式使用 the"${a[@]}"作为第一个参数,结果是:

jfaldsj 
jflajds

The second way use the "${a[*]}"as the first argument, and the result is:

第二种方式使用 the"${a[*]}"作为第一个参数,结果是:

jfaldsj 
jflajds 
LAST

Neither result is as I wished. So, is there anyone having any idea about how to pass array between functions correctly?

两个结果都不如我所愿。那么,有没有人知道如何正确地在函数之间传递数组?

回答by choroba

You cannot pass an array, you can only pass its elements (i.e. the expanded array).

您不能传递数组,只能传递其元素(即扩展数组)。

#!/bin/bash
function f() {
    a=("$@")
    ((last_idx=${#a[@]} - 1))
    b=${a[last_idx]}
    unset a[last_idx]

    for i in "${a[@]}" ; do
        echo "$i"
    done
    echo "b: $b"
}

x=("one two" "LAST")
b='even more'

f "${x[@]}" "$b"
echo ===============
f "${x[*]}" "$b"

The other possibility would be to pass the array by name:

另一种可能性是按名称传递数组:

#!/bin/bash
function f() {
    name=[@]
    b=
    a=("${!name}")

    for i in "${a[@]}" ; do
        echo "$i"
    done
    echo "b: $b"
}

x=("one two" "LAST")
b='even more'

f x "$b"

回答by Edouard Thiel

You can pass an array by reference to a function in bash 4.3+. This comes probably from ksh, but with a different syntax. The key idea is to set the -n attribute:

您可以通过引用 bash 4.3+ 中的函数来传递数组。这可能来自 ksh,但具有不同的语法。关键思想是设置 -n 属性:

show_value () # array index
{
    local -n array=
    local idx=
    echo "${array[$idx]}"
}

This works for indexed arrays:

这适用于索引数组:

$ shadock=(ga bu zo meu)
$ show_value shadock 2
zo

It also works for associative arrays:

它也适用于关联数组:

$ days=([monday]=eggs [tuesday]=bread [sunday]=jam)
$ show_value days sunday
jam

See also declare -nin the man page.

另请参见declare -n手册页。

回答by glenn Hymanman

You could pass the "scalar" value first. That would simplify things:

您可以先传递“标量”值。这将简化事情:

f(){
  b=
  shift
  a=("$@")

  for i in "${a[@]}"
  do
    echo $i
  done
  ....
}

a=("jfaldsj jflajds" "LAST")
b=NOEFLDJF

f "$b" "${a[@]}"

At this point, you might as well use the array-ish positional params directly

这时候,你还不如直接使用array-ish位置参数

f(){
  b=
  shift

  for i in "$@"   # or simply "for i; do"
  do
    echo $i
  done
  ....
}

f "$b" "${a[@]}"

回答by Lavitha

This will solve the issue of passing array to function:

这将解决将数组传递给函数的问题:

#!/bin/bash

foo() {
    string=
    array=($@)
    echo "array is ${array[@]}"
    echo "array is ${array[1]}"
    return
}
array=( one two three )
foo ${array[@]}
colors=( red green blue )
foo ${colors[@]}