If you want to pass an array by reference (which is what your code actually does), bash 4.3 allows this to be done with namevars:
foo=( hello "cruel world" )
print_contents() {
local -n array=$1
printf '%q\n' "${array[@]}"
}
print_contents foo
If you want to pass by value, even easier (and functional even with ancient releases):
foo=( hello "cruel world" )
print_contents() {
printf '%q\n' "$@"
}
print_contents "${foo[@]}"
If you want to pass multiple arrays by value, by contrast:
foo=( hello "cruel world" )
bar=( more "stuff here" )
print_arrays() {
while (( $# )); do
printf '%s\n' "Started a new array:"
local -a contents=()
local array_len
array_len=$1; shift
for ((n=0; n<array_len; n++)); do
contents+=( "$1" ); shift
done
printf ' %q\n' "${contents[@]}"
done
}
print_arrays "${#foo[@]}" "${foo[@]}" \
"${#bar[@]}" "${bar[@]}"
eval? (Why do you think you needevalto accomplish that thing?)baz "${foo[@]}"is the accepted and conventional way to pass an array by value, which makes your values accessible as"$@"inside the function. What you're actually doing is not passing by value at all, but passing the name -- which is to say, passing by reference.evalinvolved.baz "${foo[@]}"correctly, it means no other arguments can be passed to the function?