I'll try to explain:
I'm writing a bash script and I'm within a for loop. For all loops, I got a variable VAR_ID
, that is used to store an identifier for another variable that will be exported when all work is done. For each single loop, I got a variable VAL
. VAL
's value should be assigned to VAR_ID
's value. In the end, VAR_ID
's value has to be an array in order to store all values.
Phew... well, it's a bit hard to explain for me, but I think this simple script should clarify what I'm trying to do (it's narrowed down from its actual purpose of course):
#!/bin/bash
COUNT=0
VAR_ID="my_array"
declare -a "$VAR_ID"
while (( $COUNT <= 2 ))
do
VAL=$RANDOM
$VAR_ID+=( "$VAL" ) # this doesn't work
(( COUNT++ ))
done
export $VAR_ID
This should result in a variable my_array
and three random values in it. I tried using declare
as in declare $VAR_ID=$VAL
, but this doesn't work anymore if I use +=
instead of =
.
COUNT
can be used in a possible solution as position number or so if it helps as I have it also in my original script.
Thanks for any help in advance
Edit: I know there is a possibility with eval
but I try to avoid using that until there is no other way.