In the bash
shell, like in the ksh
shell whose array design bash
copied, "${array[@]}"
expands to all the distinct element of the array (sorted by array index), and "$array"
is the same as "${array[0]}"
.
So to pass all the elements of an array to a function, it's f "${array[@]}"
.
Now, a function's arguments are accessed via "$@"
, so your functions should be something like:
f() {
ls -ld -- "$@"
}
f "$a" "${b[@]}" "$c"
Another option is to pass your array by name and use named references (another feature bash
copied from ksh
(ksh93)):
f() {
typeset -n array="$1"
ls -ld -- "${array[@]}"
}
f b
Or for f
to take 3 arguments: a filename, an array name and another filename:
f() {
typeset -n array="$2"
ls -ld -- "$1" "${array[@]}" "$3"
}
f "$a" b "$c"
In virtually every other shell with arrays (csh
, tcsh
, rc
, es
, zsh
, yash
, fish
), you just use $array
to expand to all the elements of the array. In every other shell, arrays are also normal (non-sparse) arrays. A few caveats though: in csh/tcsh and yash, $array
would still be subject to split+glob, and you'd need $array:q
in (t)csh
and "${array[@]}"
in yash
to work around it, while in zsh
, $array
would be subject to empty-removal (and again "${array[@]}"
or "$array[@]"
or "${(@)array}"
works around it).