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

前端 未结 4 521
醉梦人生
醉梦人生 2020-11-27 12:55

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 functio

4条回答
  •  佛祖请我去吃肉
    2020-11-27 13:33

    This will solve the issue of passing array to function:

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

提交回复
热议问题