Sorry if I post a question on a topic discussed dozens of times. I'm trying to work on an element of an associative array and assign it to an array. I know that in bash associative arrays are monodimendional, but by using the string expansion of the quotes in bash I would assign an array. In the end I managed to do it in two steps but I don't know why I need that extra assignment and if I can avoid it.

Here's the script


 declare -A stby
 while read line; do
    stby+=(["$DS"]=" $SNAPSHOT")
done < <(awk '/stby/{print $1}' $FILE)

for ds in "${!stby[@]}"; do
 #       echo ":-) $ds"
         DATASET2=( "${stby["$ds"]}" )


         echo "The dataset list $ds contains ${#DATASET[@]} elements, i.e. ${DATASET[@]}"

DATASET2 is a string with words in it, eventually DATASET is the array I want. Is there a way to avoid the assignment DATASET=($DATASET2) and have as DATASET2 the array I want?

Extract of the file /home/user/scripts/snapstbylist

zserver02/home/GROUP/biastby@d20140123  227K    -       471M    -
zserver02/home/GROUP/biastby@d20140128  233K    -       471M    -
zserver02/home/GROUP/biastby@d20140129  206K    -       471M    -
zserver02/home/GROUP/biastby@d20140130  240K    -       471M    -
zserver02/home/GROUP/biastby@d20140131  438K    -       471M    -
zserver02/home/GROUP/biastby@d20140201  244K    -       471M    -
zserver02/home/GROUP/biastby@d20140202  220K    -       471M    -
zserver02/home/GROUP/biastby@d20140204  0       -       832M    -
zserver02/home/GROUP/bloombergstby@d20131119    0       -       148K    -
zserver02/home/GROUP/bloombergstby@initback02   0       -       148K    -
zserver02/home/GROUP/caestby@d20131228  0       -       626M    -
zserver02/home/GROUP/gtistby@d20140125  0       -       131K    -
zserver02/home/GROUP/juastby@d20140107  0       -       130K    -
zserver02/home/GROUP/kepstby@d20140118  0       -       138K    -
zserver02/home/GROUP/koastby@d20131227  93K     -       145K    -

I could sum in this way, why



DATASET2=( "${stby["$ds"]}" )

is not expanded?

Thanks in advance,


  • 1
    imho, bash has very restrictive and limited features concerning arrays. why not using a little more complete language like python/perl ?
    – mveroone
    Feb 27, 2014 at 13:12
  • @Kwaio, as soon as I complete this project I'll do, for the moment other scripts, part of the same project are in bash and hence I have to stick with it :) Feb 27, 2014 at 13:15


You must log in to answer this question.

Browse other questions tagged .