I am stuck with the below code and any help would be greatly appreciated:
I am trying to assign contents inside a dynamically generated variable to another variable. But instead of the value of the variable getting assigned, I could see that the variable name is getting assigned. I tried (echo/eval/!) but nothing is working. Could someone please help me with the solution?
Objective: To assign value 24 (saved under variable msd_ext_len) to final_count variable.
Variable Declaration
iter_val=1
var_1="msd_ext"
msd_ext_len=24
Code:
eval "final_count"=\${var_${iter_val}}_len
Output --> msd_ext_len
Expected Output --> 24
If I add one more line of code as shown below along with above code and I am getting the output:
count_val=${!final_count}
But can these 2 lines of code be clubbed into a single line of code?
iter_val
sounds like you have a bunch of different values, indexed by a number, like an array in C or Python. But then you have the variable namemsd_ext
stored in a variable too, is that something like different properties of the objects in that array, or something else? The shell doesn't really make this sort of thing easy, and I'm not sure how to interpret your example since you only show assignment to one particular value.