1

I see that similar things have been asked but no solution has worked for me yet. I have a while loop reading through some lines. I want to put certain parts of each line in an array for that line. So I'm trying to have it setup an X amount of arrays depending on how many lines come back. So I would have an array set of "logs_1, logs_2, logs_3" etc. But I keep getting syntax errors when trying to use a variable in the array name. Is there another way to create sequential arrays dynamically? I feel like I'm missing something simple here. A sample of what I'm trying is below, this was just testing to setup the arrays with the correct names.

while read line ; do
        (( n++ ));
        logs_"&n"=(test1 test2 test3)
        echo ${logs_"$n"[*]};
done

Any help is appreciated.

Raxler
  • 13
  • 6
  • Which version of `bash` do you have? Post output of `bash --version`? – Inian Mar 25 '18 at 06:47
  • Use the answer from the duplicate, that does not contain the `eval` – Inian Mar 25 '18 at 07:19
  • Thank you for pointing me to that. I did not find that solution previously. I'm using bash 4.2.46, output below. GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) While that helped (I think) to set the arrays' as logs_1, logs2, etc. I'm now having trouble getting those same arrays' called by the variable. It appears the same syntax for calling them back is not valid. This doesn't seem to be covered int he solution provided or am I missing something again? (( n++ )); declare -a "logs_${n}=('test' 'test2' 'test3')"; echo ${logs_${n}[@]}; – Raxler Mar 25 '18 at 21:21

0 Answers0