First, as you say, there are no return values of bash functions. So the only way to pass a local value is to echo
it.
However, this would lead to your targetvalue having everything you echoed in index 0 if interpreted as an array. To tell bash to treat the parts as array parts, you have to surround them by parentheses - from the bash manual:
Arrays are assigned to using compound assignments of the form
name=(value1 ... valuen), where each value is of the form [sub‐
script]=string.
#!/bin/bash
function returnarray
{
local array=(foo doo coo)
echo "${array[@]}"
}
targetvalue=($(returnarray))
echo ${targetvalue[@]}
echo ${targetvalue[1]}
However, all of this is really programming around how bash
works. It will be better to define your arrays globally.
As the use of echo
makes bash interpret the values, this only works, if the values of the array are not affected by bash, for example the values may not contain wildcards or spaces, wildcards would be expanded to matching files and spaces in a value would translate into multiple array values.