In the following code, I create some associative arrays in a loop. The consist of two strings, a string identifier and a year. After creation, I want to access the arrays in a loop based on the identifier only.#!/bin/bash# Declare associative arrays of journal-year combinationsA_JOURNAL_LIST={JF,JFE,RFS}B_JOURNAL_LIST={JBF,JFI,JMCB}ALL_JOURNAL_LIST={JF,JFE,RFS,JBF,JFI,JMCB}for year in {1998..2000} {2009..2011}do  eval A_$year=($A_JOURNAL_LIST-$year) ;  eval B_$year=($B_JOURNAL_LIST-$year) ;  eval all_$year=($ALL_JOURNAL_LIST-$year) ;done  Here I easily get bunch of arrays of the form A_1999 which e.g. expands to JF-1999 JFE-1999 RFS-1999 and so on.for journal in A B alldo  echo ${'$journal'_1999[@]}doneI expectJF-1999 JFE-1999 RFS-1999JBF-1999 JFI-1999 JMCB-1999JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999I got a bad substitution error all the time and I tried a lot of combinations. What's wrong?
Loop over associative arrays by substring
bash;array
variable indirection will be helpful here:for journal in A B alldo    indirect=${journal}_1999[@]    echo $journal: ${!indirect}doneoutputs A: JF-1999 JFE-1999 RFS-1999B: JBF-1999 JFI-1999 JMCB-1999all: JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999An eval-free rewrite. Arrays of arrays is not something bash is natively suited for, so I have to use space-separated strings and temp storage# Declare associative arrays of journal-year combinationsa_journal_list=( {JF,JFE,RFS} )b_journal_list=( {JBF,JFI,JMCB} )all_journal_list=( ${a_journal_list[@]} ${b_journal_list[@]} )declare -a a b allfor year in {1998..2000} {2009..2011}do    # store year-specific values as space-separated strings    a[$year]=$( printf %s-$year  ${a_journal_list[@]} )    b[$year]=$( printf %s-$year  ${b_journal_list[@]} )    all[$year]=$( printf %s-$year  ${all_journal_list[@]} )done  selected_years=( 1998 1999 2000 )for journal in a b alldo    # I'll use the positional params for temp storage of the accumulated array    set --    for year in ${selected_years[@]}    do        indirect=${journal}[$year]        # variable is unquoted to allow word splitting        set -- $@ ${!indirect}    done    echo $journal    printf %s\n $@done