因为除了ksh之外的shell不支持pass-by-reference,如何在不使用全局变量的情况下将多个数组传递给bash中的函数,并且允许将任何合法变量内容作为数组元素包含在内(没有保留的印记)?
答案 0 :(得分:17)
截至2016年,现代bash支持按引用传递(a.k.a nameref
属性):
demo_multiple_arrays() {
local -n _array_one=$1
local -n _array_two=$2
printf '1: %q\n' "${_array_one[@]}"
printf '2: %q\n' "${_array_two[@]}"
}
array_one=( "one argument" "another argument" )
array_two=( "array two part one" "array two part two" )
demo_multiple_arrays array_one array_two
另请参见手册页中的declare -n
。
这可以通过使用调用约定来安全地完成,该约定在每个数组之前放置参数个数,如下所示:
demo_multiple_arrays() {
declare -i num_args array_num;
declare -a curr_args;
while (( $# )) ; do
curr_args=( )
num_args=$1; shift
while (( num_args-- > 0 )) ; do
curr_args+=( "$1" ); shift
done
printf "$((++array_num)): %q\n" "${curr_args[@]}"
done
}
然后可以按如下方式调用:
array_one=( "one argument" "another argument" )
array_two=( "array two part one" "array two part two" )
demo_multiple_arrays \
"${#array_one[@]}" "${array_one[@]}" \
"${#array_two[@]}" "${array_two[@]}"
答案 1 :(得分:1)
也可以使用eval完成:
declare -a a=( "aa bb" 123 '$ $ $' )
declare -a b=( "bb cc" 456 '###' )
printf "\n%s\n" 'a before sub:'
printf "'%s'\n" "${a[@]}"
printf "\n%s\n" 'b after sub:'
printf "'%s'\n" "${b[@]}"
sub ()
{
eval a0=\${$1[0]} # get value a[0]
eval b1=\${$2[1]} # get value b[1]
echo "a[0] = '$a0'"
echo "b[1] = '$b1'"
eval $1[0]='a---a' # set value a[0]
eval $2[1]=999 # set value b[1]
} # ---------- end of function sub ----------
sub a b # call function sub
printf "\n%s\n" 'a after sub:'
printf "'%s'\n" "${a[@]}"
printf "\n%s\n" 'b after sub:'
printf "'%s'\n" "${b[@]}"
输出:
a before sub:
'aa bb'
'123'
'$ $ $'
b after sub:
'bb cc'
'456'
'###'
a[0] = 'aa bb'
b[1] = '456'
a after sub:
'a---a'
'123'
'$ $ $'
b after sub:
'bb cc'
'999'
'###'