0

Suppose I have several variable names like

V_1_T = a
V_2_T = b
V_3_T = c
...

and I want to extract the pointers a, b , c, ... in a bash loop in order to concatenate the values. My explicit wish is about reconstructing a message separated in several parts as explained in the gammu-smsd documentation. I've tried the example in the doc but it doesn't work. The reason is that the code never points to the pointer of the variables but to the variables themselves, i.e. I get V_1_T at best and never a as I would.

I've also tried to put

${V_${i}_T} ; ""$"V_${i}_T"

with and without escape symbols for the commas, ..., but nothing worked ...

Any ideas ?

I'm working on the latest version of Raspbian + RaspberryPi.

FraSchelle
  • 249
  • 4
  • 10

1 Answers1

2

Use indirect parameter expansion:

for i in 1 2 3; do
  t="V_${i}_t"
  echo "${!t}"
done

This avoids the use of eval shown in the docs you linked to.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • excellent, thanks a lot. But i guess it was not a problem with `eval` am I correct ? Rather it has to do with my ignorance in bash isn't it ? – FraSchelle Jun 07 '18 at 17:24
  • 1
    @FraSchelle, ...for discussion of why `eval` use is undesirable, see [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) – Charles Duffy Jun 07 '18 at 17:27