从 Bash 函数返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14839199/
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-18 04:32:20  来源:igfitidea点击:

Returning array from a Bash function

arraysbashfunctionshellreturn-value

提问by KayKo

I am making a bash script and I have encountered a problem. So let's say I got this

我正在制作一个 bash 脚本,但遇到了一个问题。所以让我们说我得到了这个

function create_some_array(){
  for i in 0 1 2 3 .. 10
  do
    a[i]=$i
  done
}

create_some_array
echo ${a[*]}

Is there any way I can make this work? I have searched quite a lot and nothing I found worked. I think making the a[]a global variable should work but I can't find something that actually works in my code. Is there any way to return the array from the function to main program?

有什么办法可以使这项工作?我已经搜索了很多,但我发现没有任何效果。我认为创建a[]一个全局变量应该可以工作,但我在我的代码中找不到实际工作的东西。有没有办法将数组从函数返回到主程序?

Thanks in advance

提前致谢

回答by that other guy

This works fine as described. The most likely reason it doesn't work in your actual code is because you happen to run it in a subshell:

这如描述的那样工作正常。它在您的实际代码中不起作用的最可能原因是因为您碰巧在子 shell 中运行它:

cat textfile | create_some_array
echo ${a[*]}

would not work, because each element in a pipeline runs in a subshell, and

将不起作用,因为管道中的每个元素都在子外壳中运行,并且

myvalue=$(create_some_array)
echo ${a[*]}

would not work, since command expansion happens in a subshell.

将不起作用,因为命令扩展发生在子 shell 中。

回答by didierc

You can make an array local to a function, and then return it:

您可以将数组设为函数的局部数组,然后返回它:

function create_some_array(){
    local -a a=()
    for i in $(seq  ); do
        a[i]=$i
    done
    echo ${a[@]}
}

declare -a a=()

a=$(create_some_array 0 10)

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

回答by Hans

This won't work as expected when there are whitespaces in the arrays:

当数组中有空格时,这将无法按预期工作:

function create_some_array() {
    local -a a=()
    for i in $(seq  ); do
        a[i]="$i $[$i*$i]"
    done
    echo ${a[@]}
}

and worse: if you try to get array indices from the outside "a", it turns out to be a scalar:

更糟糕的是:如果您尝试从外部“a”获取数组索引,结果是一个标量:

echo ${!a[@]}

even assignment as an array wont help, as possible quoting is naturally removed by the echo line and evaluation order cannot be manipulated to escape quoting: try

即使作为数组赋值也无济于事,因为可能的引用会被 echo 行自然删除,并且无法操纵评估顺序来逃避引用:尝试

function create_some_array() {
...
    echo "${a[@]}"
}

a=($(create_some_array 0 10))
echo ${!a[@]}

Still, printf seems not to help either:

尽管如此, printf 似乎也无济于事:

function create_some_array() {
...
    printf " \"%s\"" "${a[@]}"
}

seems to produce correct output on one hand:

一方面似乎产生正确的输出:

$ create_some_array 0 3; echo
 "0 0" "1 1" "2 4" "3 9"

but assignment doesn't work on the other:

但分配不适用于另一个:

$ b=($(create_some_array 0 3))
$ echo ${!b[@]}
0 1 2 3 4 5 6 7

So my last trick was to do assignment as follows:

所以我的最后一个技巧是按如下方式进行分配:

$ eval b=("$(create_some_array 0 3)")
$ echo -e "${!b[@]}\n${b[3]}"
0 1 2 3
3 9

Tataaa!

塔塔!

P.S.: printf "%q " "${a[@]}" also works fine...

PS: printf "%q " "${a[@]}" 也可以正常工作......

回答by Luis Tiago Flores Cristóv?o

Hi here is my solution:

嗨,这是我的解决方案:

show(){
    local array=()
    array+=("hi")
    array+=("everything")
    array+=("well?")
    echo "${array[@]}"
}

for e in $(show);do
    echo $e
done

Try this code on: https://www.tutorialspoint.com/execute_bash_online.php

试试这个代码:https: //www.tutorialspoint.com/execute_bash_online.php