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:
f(){
x=($1)
y=$2
for i in "${x[@]}"
do
echo $i
done
....
}
a=("jfaldsj jflajds" "LAST")
b=NOEFLDJF
f "${a[@]}" $b
f "${a[*]}" $b
As described, function f
receives two arguments: the first is assigned to x
which is an array, the second to y
.
f
can be called in two ways. The first way use the "${a[@]}"
as the first argument, and the result is:
jfaldsj
jflajds
The second way use the "${a[*]}"
as the first argument, and the result is:
jfaldsj
jflajds
LAST
Neither result is as I wished. So, is there anyone having any idea about how to pass array between functions correctly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…