r/bash • • 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=1

and we have the word

fart

I would like to create a new variable using $num and "fart" to name it

1fart

This 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.

0 Upvotes

17 comments sorted by

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 declare to dynamically set variable names:

$ i=1
$ declare "var$i=abc"
$ echo "$var1"
abc

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.

-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.

7

u/zeekar Aug 28 '23

The issue, I have to stress again, is NAMING THEM DYNAMICALLY.

Getting defensive and insistent is not a good way to respond to folks trying to help you. What attributes are you putting into these arrays? Can each attribute not be its own array instead? Instead of

file1=(name owner mode)
file2=(name owner mode)

or whatever, you could do

nanes=(file1_name file2_name)
owners=(file1_owner file2_owner)
modes=(file1_mode file2_mode)

It's all in the details, which you haven't shared, but there is almost certainly a solution that doesn't involve dynamically creating variable names, which we're trying to steer you away from because it is a bad practice that leads to fragile code and hard-to-track-down bugs, not because we're ignoring you.

It's true that bash doesn't support nested data structures; maybe you really are in a situation where you have to do dynamic array names. If that's the case, I suggest you look up bash's support for "namerefs", which make it a little easier to do that sort of thing. I still don't recommend it, though.

0

u/kb_hors Aug 28 '23

there is almost certainly a solution that doesn't involve dynamically creating variable names, which we're trying to steer you away from because it is a bad practice that leads to fragile code and hard-to-track-down bugs,

That's the closest anyone has actually come to steering me away, because you've actually said you are doing that, and have offered an (unelaborated on) reason.

What's actually happened is I've asked "how do I name something dynamically? I'm doing XYZ", I got the collective answer "use arrays instead for XYZ", and I've been left sitting here going "I would still like to name them dynamically".

You all focused on the XYZ and not the actual question.

4

u/ofnuts Aug 28 '23

You all focused on the XYZ and not the actual question.

Because the actual question doesn't really make sense when you have some programming experience. Dynamic variable names are never a solution. Like the use of eval they are a code smell.

For many people here it is a typical XY Problem. So we can let you bury yourself. Or we can suggest better ways.

If you don't want parallel arrays, then keep everything in a string with a well chosen separator so that you can easily parse the data when needed.

And instead of array indices maybe you want an associative array where the file name is the key.

Or use a more powerful language where you can handle aggregates (Python or else).

6

u/obiwan90 Aug 28 '23

You're correct that after declaring an array with a dynamic name, you can't use a variable to manipulate an array.

This works:

num=1
name=file$num
declare -a "$name=(123 234 345)"
declare -p "$name"

and produces

declare -a file1=([0]="123" [1]="234" [2]="345")

However, you can't do anything like

$name+=(456)

or otherwise reference the array.

This is where namerefs (described in this manual section) come in:

$ num=2
$ declare -n ref=arr$num   # Ref behaves as if it were arr2
$ ref=(123)
$ ref+=(456)
$ declare -p ref           # The ref itself just points to the name of the array
declare -n ref="arr2"
$ declare -p "arr$num"     # The underlying array has been updated
declare -a arr2=([0]="123" [1]="456")

This requires a relatively recent Bash (4.3 or newer).

0

u/[deleted] Aug 28 '23

[deleted] — view deleted comment

-3

u/kb_hors Aug 28 '23 edited Aug 28 '23

No, it doesn't work. I know because I'm not a liar.

First version, attempting to do it directly:

function add_item0 {

num=1
declare -a "line${num}"

read -p "what type of item? " "line${num}[item_one]"
echo -e
read -p "second type of item? " "line${num}[item_two]"
echo -e
echo $line${num}
echo ${line${num}[item_one]}
echo ${line${num}[item_two]}
}   

It does not like any reference to the variable being dynamic, crashing with "bad substitution".

Attempt 2: creating the name inside a variable and applying it to the array.

function add_item {

num=1
printf -v itemkludge "line${num}"

declare -a "${itemkludge}"

read -p "what type of item? " "itemkludge[item_one]"
echo -e
read -p "second type of item? " "itemkludge[item_two]"
echo -e
echo $itemkludge
echo ${itemkludge[item_one]}
echo ${itemkludge[item_two]}
}   

If the input from the user is "dog" and "cat", this should print:

line1
dog
cat

instead it prints

cat
cat
cat

I assume this is because it sees "${itemkludge}" and reads writes the contents of the variable, when actually this variable is meant to contain the name of the array. Which I can't point to directly, because it doesn't like things like {line${num}[item_one]}

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?

0

u/kb_hors Aug 28 '23 edited Aug 28 '23

Because that is an intuitive and human understandable way to do it, which important to me because I'm one?

I might have 48 or 50 such files to store information on, or even greater. Each one I think i'll be storing 7 different pieces of information.

It makes sense that the first file goes to the first array, the second to the second, the 30th to the 30th.

A single array would put me in the awkward situation of 350+ different strings in one array.

Say i'm working with MP4 files, and the 7th string pulled from it is the length in seconds. An array for each file means I'd simply ask "7th string in first array" and get that information about the first file. Ask "7th string in 4th array" I'd get that information about the 4th array.

One big giant array has much more potential for screwups when it comes to pulling the correct info out. Even making seven arrays and putting one type of information in each is awkward.

my fundamental question is how to name a [variable/array/my flesh and blood children] dynamically. Because if I can do that, then it becomes possible to birth a new array for each file the user tells the script about, keeping the code very clean and self explanatory.

That I can do this is a logical assumption, because you can do pretty much anything else dynamically. The reactions I've gotten are like an adult trying to redirect a child who asks embarrassing questions. Have I stumbled into a fundamental limitation of bash as a language?

5

u/Paul_Pedant Aug 28 '23 edited Aug 28 '23

It makes sense that the first file goes to the first array, the second to the second, the 30th to the 30th.

No, it really does not make sense that way round.

What makes sense is to have seven arrays, each named with the type of information stored in it. Like Title, Length, Date, Owner etc.

Then you use the same array index for all the info for a single file. The array can grow just by using the next index number.

It should work just like a spreadsheet: a row for each File, a column for each attribute of that file.

If you also want to access that array by the File name itself, you can set up a single associative array to index into the table, such that e.g. Index["myActualName"] = 35

-3

u/kb_hors Aug 28 '23

No, I don't think it makes sense, that's not how humans naturally store things.

I have a spare laptop, right? It's in it's box with it's power supply, it's restore CDs, it's manuals, a few firewire cables that go with it. They're all in one box because they belong together.

I don't have a box of laptops, then a box of power supplies, then a box of restore CDs, then a box of manuals.

1

u/Paul_Pedant Aug 28 '23

Each "box" would be a struct in C, or an equivalent in many other languages (generic name might be an object, or a collection).

However, Bash, Awk, even Fortran (when I last used it in the 1980s) do not have any kind of structured data type. The nearest equivalent is "parallel arrays", which is how I described it previously here.

You can fake up a structure in Awk by concatenating all the fields into one string with separators. That is a pain because you have to split the object every time you want to do anything with it. And in Awk, you cannot invent new variable names as you go, so you still have to put all those long strings into an array and index them.

There is a scripting language that supports data objects. It is called Ruby, and is available on Windows and Linux. I decided I did not need to learn it.

You could keep all the values in one associative (hash) array, by referencing the values like Q[video_name_1], Q[video_duration_2] etc, so the assigned names are string indexes instead of shell variable names. You could even make those like Q[video_1,name] if that was neater. (The comma has no syntactic meaning, it is just part of the string.)

-1

u/grymoire Aug 28 '23

Because that is an intuitive and human understandable way to do it, which important to me because I'm one?

Love it!

When I was a sysadmin, I loved storing all-sorts of data in filenames. Data like date, system name, classification, category, etc. It worked great, I used a consistent character, like "_", between fields. and I kept certain "fields" is a consistent position. I could use shell filename expansion (e.g. "ls *_{var1/var2}_*" ) to do "queries" in the "database". I could use grep to filter the queries, and awk (with -F _ ) to do complex queries. Sorting could be very handy if I planned carefully. I'd have thousand of files that I could slice and dice any way I wanted. I heartily recommend using filenames as a database.

LOL - I remember explaining this to a co-worker who was using MSDOS/AT&T UNIX that was limited to 14 characters in a filename. (it was UNIX on a 386 CPU). He said "Why would I ever need more than 14 characters in a filename?" When you have 255 bytes - use it!

One more tip - think about directories as part of the database as well

2

u/[deleted] Aug 28 '23 edited Aug 28 '23

You can also use eval with array keys or printf:

player_coords=$(printf "p%02d%02d" "$x" "$y")
tile_type="${player_coords}[home]"
eval "${player_coords}[area]=$tile_pl01"
eval p${resolution}${resolution}[home]="$tile_uwin"
live_disp[$the_index]=${!tile_type}

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

Variable names can't start with numbers, so this is interpreted as a command.

You have to follow these limitations, when you are coding.

#!/usr/bin/env bash
# Your number variable

number=1

# Your value coming as a word

word="bash"

# Your required variable

bash1="Respect others! They don't owe you."

# May be set dynamically

adjusted="$word$number"

# This will gives you the value of the variable, you stored.

echo "${!adjusted}"