In a bash script, I would like to put the following code that assigns values to each element of several arrays into a function
for (( i=0 ; i < ${#themes[@]} ; i+=1 )); do
c_bit_mins[i]=-5
c_bit_maxs[i]=15
gamma_bit_mins[i]=-15
gamma_bit_maxs[i]=3
done
i.e. something like
function set_values()
{
for (( i=0 ; i < ${#themes[@]} ; i+=1 )); do
c_bit_mins[i]=-5
c_bit_maxs[i]=15
gamma_bit_mins[i]=-15
gamma_bit_maxs[i]=3
done
}
How to do it? Especially when these arrays are not seen as global inside the function.
Thanks and regards!
You can make a variable local by using the
localcommand:However, you can’t “return” an array out of a shell function. The return value of a shell function is always an integer. Non-integer values are typically “returned” by echoing them and reading them back in using
$(...)in the surrounding program. But that will be completely weird to do with arrays and four of them.