Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - A variable as part of array name (part 2)

Tags:

arrays

bash

Let's say I start with just item1=('item1' '1' '20')

I then define itemnumber=2

I would like to create the next array as item$itemnumber=('item2' '4' '77'), but I get a syntax error.

After that I would like to just do itemnumber=$((itemnumber+1)), and create item$itemnumber=('item3' '17' '15')

Which would give me three arrays item1, 2 and 3:

item1=('item1' '1' '20')
item2=('item2' '4' '77')
item3=('item3' '17' '15')

Is this possible?

like image 259
Alichino Avatar asked Feb 14 '26 09:02

Alichino


1 Answers

A task for eval:

itemnumber=1
(( itemnumber += 1))
eval "item$itemnumber=('item$itemnumber' '4' '77')"
eval echo \${item$itemnumber[*]} 
like image 78
Juan Diego Godoy Robles Avatar answered Feb 16 '26 00:02

Juan Diego Godoy Robles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!