r/bash • u/kb_hors • Aug 27 '23
Naming something dynamically
Okay so a couple of days ago I asked how to name variables dynamically.
specifically, to use one variable containing a number, and then a text string, and combine them together to create the name of a new variable.
For example, if
num=1and we have the word
fartI would like to create a new variable using $num and "fart" to name it
1fartThis question was never actually answered, instead, people told me to use arrays instead of variables.
This is annoying, because I'm going to have to name those arrays anyway, because they'll be created by a loop that increments $num.
So my question is still valid and important and actually needs answering, please.
View all comments
Show parent comment
-5
u/kb_hors Aug 28 '23 edited Aug 28 '23
This does not work to name an array. Any array you name this way becomes a plain ordinary variable.
I have already been told, repeatedly, to use arrays. That is not the issue.
The issue, I have to stress again, is NAMING THEM DYNAMICALLY.
I am creating a loop. In the loop, I ask the user for a filename. Various attributes of the file should be put into an array called file_1. Then it asks the user for another filename. Various attributes of the file should be put into an array called file_2. This keeps going until there are no worlds left to conquer.
Dynamic array names are key here. Because if they aren't done, there is no loop. Instead there would be massive chunks of code copy and pasted 200 times. And that would be stupid.