2

If we:

  1. Define an array; and then..
  2. Define a function; and want to..
  3. Call that array from inside the function..

We can. Like so:


Input:

myArray=('1' '2' '3' '4' '5')

myFunction () 
{ 
local -n myList="$1";
echo "${myList[@]}";
}

myFunction myArray

Output:

1 2 3 4 5

So, ${myArray[@]} becomes ${myList[@]}, within the scope of myFunction.

But this method only works as of bash version 4.3.x.

How did (and how can) we do this with older versions?

3

In older bashes, you need "variable indirection" (4th paragraph of Shell Parameter Expansion), which is really ugly for arrays:

myArray=('1' '2' '3' '4' '5')
myFunction() {
    local arr="${1}[@]"         # array expansion *as a string*
    local values=( "${!arr}" )  # actual array expansion
    echo "${values[@]}"
}
myFunction myArray
1 2 3 4 5

Note that this gives you a copy of the array. Any modifications you make in the function will not alter myArray in the outer scope:

myFunc2_old () {
    local arr="${1}[@]"
    local values=( "${!arr}" )  # here's the copy
    values[0]=foo
    declare -p values
}
myFunc2_old myArray; declare -p myArray
declare -a values=([0]="foo" [1]="2" [2]="3" [3]="4" [4]="5")
declare -a myArray=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")

As opposed to namerefs

myFunc2_ref () {
    local -n arr=$1
    arr[0]=foo
    declare -p arr
}
myFunc2_ref myArray; declare -p myArray
declare -n arr="myArray"
declare -a myArray=([0]="foo" [1]="2" [2]="3" [3]="4" [4]="5")
  • Thanks, let me just wrap my mind around that for a minute.. Is it both local arr="${1}[@]" AND local values=( "${!arr}" )? Or just one or the other? – tjt263 Dec 7 '18 at 3:01
  • You need both. The first statement sets arr="myArray[@]" -- that's just a string. Then ${!arr} becomes ${myArray[@]} with indirect expansion. Then with the parentheses and double quotes, you get the elements of the array, forming the copy. – glenn jackman Dec 7 '18 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.