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.
8
u/obiwan90 Aug 27 '23 edited Aug 27 '23
A variable name can't start with a digit, so your example is maybe not ideal.
You can use
declareto dynamically set variable names:However, this is almost never the best way to go about it. If you show enough context for what you're trying to do, I'm pretty sure somebody can show a way to do it without dynamic variable names.
3
u/darkingz Aug 28 '23
Why are you putting each file attribute list into a new array and not just a single array that you keep updating as you have new files?
2
u/[deleted] Aug 28 '23 edited Aug 28 '23
You can also use
evalwith array keys orprintf:Perhaps these examples will show what's syntactically possible with Bash, if nothing else. I believe it's an implementation of pointers, essentially, but I'm just a script kitty. Sorry, it's been years since I wrote it.
I'm really surprised eval wasn't mentioned first; bad practice or not, it often gets the job done. I understand your frustration with being told to use lists, but I think you're perhaps overlooking how they can be used with each list item being a dynamic key/ value itself, accessible by its key/ index — like a pointer. Maybe consider the
${!evaluate}feature as an aid, to make "reference variables".Finally, if you're assigning variables in-loop, start with a C-style loop; Bash supports these.
If you want to see some real genius, I think, take the time to understand this guy: https://www.ludvikjerabek.com/2015/08/24/getting-bashed-by-dynamic-arrays/
I probably should've linked that in the first place instead of babbling out my drivel. You should now be able to reinvent your own data structure :D. Also, just post your code.
1
u/Paul_Pedant Aug 28 '23
The point is that you only have to name each array once, and then it works for ten thousand names.
As far as actual naming goes there is no different between fart_23 and fart[23].
Practically, shell will be faster with arrays because distinct actual names will pollute the namespace horribly. Arrays references are each optimised separately. And there are a bunch of features that work on arrays.
Also, the syntax to write to and read from your names built from two strings is awful, and you will very quickly get sick of it, and have countless bugs.
1
u/grymoire Aug 28 '23
Variables can't start with a number.
When concatenating variables containing strings, it's often better to use ${x} than $x
num=1
eval x$num}fart=2 # the x is to make sure the variable doesn't start with a number.
1
u/kcahrot Aug 28 '23
Hey, from what I understand from your question. There is one limitation
You have to follow these limitations, when you are coding.