So I discovered that the most convenient way to pass named arguments to bash functions is via 'temporary env variables' like so: kwd_arg=1 foo. And I want to use this to pass array variables but apparently these two features don't mix as expected & I want to know how to use them together properly.
I've tried the same syntax without a function involved & without 'temporary assignment': both worked. However when used together the array arg is treated as a plain string.
# bash func accepting an env kwd arg
foo() {
echo ${array_arg[0]}
echo ${array_arg[1]}
}
# doesn't work, array arg treated as string
array_arg=(1 2) foo
# output:
# (1 2)
#
# when set globally works surprisingly
array_arg=(1 2)
foo
# output:
# 1
# 2
# works of course
echo ${array_arg[0]}
echo ${array_arg[1]}
# output: same as above
malloc()ed by the running process. Please be specific about exactly which part of your example code shows bash arrays stored in the environment specifically.foo() { local -n theArray=$1; echo "${theArray[0]}"; }then initialize the array and pass the array name to the function:array_arg=(1 2); foo array_arg-- however, you must use a different variable name or you'll see a "circular reference" error.read -r -a items <<<"$items_str"; for item in "${items[@]}"; do ...; done) on the receiving end. (for item in $itemsis buggy for reasons touched on in BashPitfalls #1; those issues can be mitigated by turning off globbing before expansion).