[bash] Array inside associative array?

So, i have learned a cool trick of having associative arrays in bash from here, which leads in pretty clean data structures (if you're into it)...



declare -A themes0=(

    # Theme

    # Plasma theme
    [color_scheme]="Test Dark Blue"

    # QT theme

    # GTK theme

declare -A themes1=(

    # Theme

    # Plasma theme
    [color_scheme]="Test Light Blue"

    # QT theme

    # GTK theme

declare -n themes

main() {

    for themes in "${!themes@}";
        echo "${themes[plasma_theme]}"

        for key in "${!themes[@]}";
            echo "${key}:   ${themes[$key]}"


main "$@"

Now, i'd like to know how to effectively do something like that (which sadly seems to be impossible directly in current bash):


declare -A themes0=(


        # Each line (except comments and empty ones) should be parsed as array later
        "Some/Path1/file1.test" "Some/Path1/file2.test"

        # Yep, array too
        "Some/Path2/file3.test" "Some/Path3/file4.test" "Some/Path4/file5.test"

Thoughts / tips & tricks are welcome :slight_smile:


declare -A themes0=(

    [files]="Some/Path1/file1.test Some/Path1/file2.test" 

for key in "${!themes0[@]}";
    if [[ "${key: -1}" == "s" ]]; then  # its a array (for me) !
        echo "${key}:"
        for v in "${values[@]}"; do echo "  - $v"; done
        echo "${key}:   ${themes0[$key]}"

EDIT: if datas fixed, we can use conf file json(use jq) or yaml(exemple)

1 Like

its a array (for me) !


Nice one, but i maybe showed a little too reduced example, my bad...

array_in_array will certainly have:

  1. Some complex stuff like that:
'"$HOME/.local/share/konsole/133Matrix.colorscheme" "General" "Opacity" $(echo "scale=2; ${opacity}/100" | bc)'
  1. All paths should certainly be quoted (since it can and will have spaces), therefore simple string to array is not really possible...And your example will fail on spaced path

usually i overcome this problem with your previous advices on my question, like that:


    # Bluetooth disable
    '"bluedevilglobalrc" "General" "enableGlobalBluetooth" false'

    # ... etc

for config in "${configBluetooth[@]}";
    configWrite "$config"

But when working inside another array quoting each item with ' ' is not an option it seems...

  1. I d like to have specifically multiline style of array, like in op abstract example, so that i could treat each line as array item later with something like
declare -a "config=($(printf '%s\n' "$config"))"

Wow, that's really cool!

But sadly....

In theory, can json / yaml be declared dynamically inside bash file and then parsed?
Preferably without creating temp files (although it's easy with something like that):

cat >> $HOME/data.json<<"EOL"

for space separator, we can change the field separator (here # for example) :

[files]="Some/Path1/file 1.test#Some/Path1/file 2.test" 
IFS='#' values=(${themes0[$key]})    # or IFS="\t" ...
for v in "${values[@]}"; do echo "  - $v"; done

for me "fixed" was : you dont change list in script (remove,edit...)

Oh ok...Well i do in one place now, but it can be avoided!

Yeah, you have actually sparked some thought in me about IFS...
I'm not a big fan of IFS in general, for it's somewhat destructive behavior (in case you forget to change it after some trickery)

here we not change IFS for all script but only for the line :wink:
EDIT: as a LANG=de pacman -Qi package


Forum kindly sponsored by