How to pass array as an argument to a function in Bash
Solution 1:
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=$1[@]
b=$2
a=("${!name}")
for i in "${a[@]}" ; do
echo "$i"
done
echo "b: $b"
}
x=("one two" "LAST")
b='even more'
f x "$b"
Solution 2:
You can pass an array by name reference to a function in bash (since version 4.3+), by setting the -n
attribute:
show_value () # array index
{
local -n myarray=$1
local idx=$2
echo "${myarray[$idx]}"
}
This works for indexed arrays:
$ shadock=(ga bu zo meu)
$ show_value shadock 2
zo
It also works for associative arrays:
$ declare -A days=([monday]=eggs [tuesday]=bread [sunday]=jam)
$ show_value days sunday
jam
See also nameref
or declare -n
in the man page.
Solution 3:
You could pass the "scalar" value first. That would simplify things:
f(){
b=$1
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
f(){
b=$1
shift
for i in "$@" # or simply "for i; do"
do
echo $i
done
....
}
f "$b" "${a[@]}"