-1

I want to create variable from few different strings. I am looping by some list of strings - systemvar. The next thing I want to do is to append some data (another strings) to variable created by looping the list of strings.

In this case it would be s1_bookmark_systemA, s1_bookmark_systemB and so on.

My code is:

#!/bin/bash
looping="a b"
systemvar="SystemA SystemB"
dc="s1"

for i in $looping; do
    for j in $systemvar; do
          line=""$i" some other variable"
          echo "line:             " $line
          bookmark="$dc""_bookmark_""$j"
          echo "created variable: " $bookmark
          "${!bookmark}"+=$line ### appending to s1_bookmark_systemA, then appending to s1_bookmark_systemB and so on
          echo -e "${!bookmark}"
    done
done

echo $s1_bookmark_SystemA
echo $s1_bookmark_SystemB

I was assuming that would append $line to s1_bookmark_systemA as long as I am in loop. But I get:

line:              a some other variable
created variable:  s1_bookmark_SystemA
./tester2.sh: line 12: +=a: command not found

line:              a some other variable
created variable:  s1_bookmark_SystemB
./tester2.sh: line 12: +=a: command not found

line:              b some other variable
created variable:  s1_bookmark_SystemA
./tester2.sh: line 12: +=b: command not found

line:              b some other variable
created variable:  s1_bookmark_SystemB
./tester2.sh: line 12: +=b: command not found

Ideas?

Hubert
  • 27
  • 8
  • 2
    Use https://www.shellcheck.net/ as a first pass for debugging your script. There look to be errors with double quoting variables and one error with the `+=` – j_b Jun 01 '22 at 18:31
  • 2
    Use arrays: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html – Diego Torres Milano Jun 01 '22 at 18:32
  • 2
    Instead of variable variables, why not use an associative array? That's generally the best solution in any language that has them. – Barmar Jun 01 '22 at 18:33
  • 1
    Does this answer your question? [Creating a string variable name from the value of another string](https://stackoverflow.com/questions/13716607/creating-a-string-variable-name-from-the-value-of-another-string) – pjh Jun 01 '22 at 19:50
  • 1
    Also see [How can I generate new variable names on the fly in a shell script?](https://stackoverflow.com/questions/10820343/how-can-i-generate-new-variable-names-on-the-fly-in-a-shell-script) and [Dynamic variable names in Bash](https://stackoverflow.com/q/16553089/4154375). – pjh Jun 01 '22 at 19:53
  • 2
    [BashFAQ/006 (How can I use variable variables (indirect variables, pointers, references) or associative arrays?)](https://mywiki.wooledge.org/BashFAQ/006) might also be useful. – pjh Jun 01 '22 at 19:55
  • 1
    You can't have parameter substitution on the left-hand side of a variable assignment. Therefore `"${!bookmark}"+=$line ` is not understood as assignment. Aside from this: Wouldn't it make more sense to use arrays in your case? – user1934428 Jun 02 '22 at 07:35
  • Thank you, arrays is the answer. I knew about arrays, but I have no idea where can I use them. And finally when I encounter that problem I see a real use case for arrays. – Hubert Jun 02 '22 at 10:14

0 Answers0