Question

I am trying to pass an associative array from one function to another, and am losing named index keys (e.g., filepath, search in example below) though the array passed in can access its elements correctly using indexes 0, 1. I must be doing something slightly wrong with bash syntax, but can't quite figure out where. Any help appreciated.

Using GNU bash, version 4.3.8 from Ubuntu 14.04 Just below is bash code example, and at bottom is output

#! /bin/bash

function test_function {

    func_data=("${@}")
    # without brackets above cannot access func_data[1]

    # local ${!func_data}
    # the above local statement does not seem to help either way

    echo ""
    for K in "${!func_data[@]}"; do echo $K; done

    echo ""
    echo "func_data           : ${func_data}"
    echo "func_data[filepath] : ${func_data[filepath]}"
    echo "func_data[search]   : ${func_data[search]}"
    # all three echos above output first element of array, 
    # which is 'style "default" {' during first loop

    # Can access array elements 0, 1 but no longer via filepath, search
    echo "func_data[0]        : ${func_data[0]}"
    echo "func_data[1]        : ${func_data[1]}"

    echo "!func_data[@]       : ${!func_data[@]}"
    # echo above outputs '0 1' so indexes now are now zero based?
    echo "func_data[@]        : ${func_data[@]}"
    # echo above outputs all array elements 'style "default" { ~/.gtkrc-2.0'
}

# In BASH, local variable scope is the current function and every 
# child function called from it, so provide a function main to
# make it possible to utilize variable scope to fix issues

function main {

    echo ""
    declare -A gtkrc2=()
    gtkrc2[filepath]="~/.gtkrc-2.0"
    gtkrc2[search]="style \"default\" {"
    echo "gtkrc2 filepath : ${gtkrc2[filepath]}"
    echo "gtkrc2 search   : ${gtkrc2[search]}"
    test_function "${gtkrc2[@]}"

    echo ""
    declare -A gtkcss=()
    gtkcss[filepath]="~/.config/gtk-3.0/gtk.css"
    gtkcss[search]=".scrollbar {"
    echo "gtkcss filepath : ${gtkcss[filepath]}"
    echo "gtkcss search   : ${gtkcss[search]}"
    test_function "${gtkcss[@]}"
}

main

---------- OUTPUT ----------

gtkrc2 filepath : ~/.gtkrc-2.0
gtkrc2 search   : style "default" {

func_data           : style "default" {
func_data[filepath] : style "default" {
func_data[search]   : style "default" {
func_data[0]        : style "default" {
func_data[1]        : ~/.gtkrc-2.0
!func_data[@]       : 0 1
func_data[@]        : style "default" { ~/.gtkrc-2.0

gtkcss filepath : ~/.config/gtk-3.0/gtk.css
gtkcss search   : .scrollbar {

func_data           : .scrollbar {
func_data[filepath] : .scrollbar {
func_data[search]   : .scrollbar {
func_data[0]        : .scrollbar {
func_data[1]        : ~/.config/gtk-3.0/gtk.css
!func_data[@]       : 0 1
func_data[@]        : .scrollbar { ~/.config/gtk-3.0/gtk.css
Was it helpful?

Solution

This may or may not be the "correct" way to do this but this is the best I can figure out. Any suggestions from others are welcome:

function test_function {
    arrname=$1
    idxlist="$2"

    echo ""
    echo "Array passed=$arrname"
    for idx in $idxlist; do
        elemname=$arrname[$idx]
        echo "idx=$idx, elem=${!elemname}"

    done
}

# In BASH, local variable scope is the current function and every 
# child function called from it, so provide a function main to
# make it possible to utilize variable scope to fix issues

function main {

    echo ""
    declare -A gtkrc2=()
    gtkrc2[filepath]="~/.gtkrc-2.0"
    gtkrc2[search]="style \"default\" {"
    echo "gtkrc2 filepath : ${gtkrc2[filepath]}"
    echo "gtkrc2 search   : ${gtkrc2[search]}"
    test_function gtkrc2 "${!gtkrc2[*]}"

    echo ""
    declare -A gtkcss=()
    gtkcss[filepath]="~/.config/gtk-3.0/gtk.css"
    gtkcss[search]=".scrollbar {"
    echo "gtkcss filepath : ${gtkcss[filepath]}"
    echo "gtkcss search   : ${gtkcss[search]}"
    test_function gtkcss "${!gtkcss[*]}"
}

main

In particular:

  • To pass each associative array to the function, we pass both the name of the array, and its list of indices
  • Inside the function, the array name and index list are taken from the positional parameters
  • We may then loop over the list of indices and obtain the corresponding value of each element. This is done by first generating the name of the element, and then using the ! indirection modifier to get the actual value.

This technique of indirection of arrays is described in this question, but only addresses indexed arrays, and not associative arrays; passing the index list is one way I can think of to get this to work for associative arrays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top