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
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:
! 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With