-2

All is in the title, this formula permit to show the value of {!i} but not to modify it, do you have any idea ?

wxi
  • 7
  • 4
  • 1
    Possible duplicate of [Indirect variable assignment in bash](https://stackoverflow.com/questions/9938649/indirect-variable-assignment-in-bash) – Socowi Feb 08 '19 at 14:42
  • ⚠️ **Please stop posting (and upvoting) answers!** All three answers posted here are duplicates of answers to [this seven year old question](https://stackoverflow.com/questions/9938649/indirect-variable-assignment-in-bash). If you have a genuinely new answer, post it there, not here. – Socowi Feb 09 '19 at 13:43

3 Answers3

3

Use printf -v.

$ x=foo
$ foo=5
$ printf -v "$x" '%d' 9
$ echo "$foo"
9
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I downvoted because this answer is a duplicate of https://stackoverflow.com/a/16973754/6770384. – Socowi Feb 09 '19 at 13:41
-1

Use declare:

$ foo=bar
$ declare $foo=hello
$ echo $bar
hello 
Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • 1
    I downvoted because this answer is a duplicate of https://stackoverflow.com/a/11460242/6770384. – Socowi Feb 09 '19 at 13:40
  • And that makes the answer bad and less helpful? With this attitude, less answers will be posted, because they may be duplicates of something old. Anyway, I don't change my behavior. – Wiimm Feb 09 '19 at 13:56
  • 1
    This answer got posted *after* the hint *"possible duplicate of"* was there. Also I think each user should research whether a question is an *exact* duplicate before answering - especially for basic questions like this. – Socowi Feb 09 '19 at 14:05
-2

Mostly used syntax are as others answer. A less interesting answer would be to use read:

foo=5
i="foo"
echo "${!i}"
# yields "5"

read -r "$i" <<< 10
echo "${!i}"
# yields "10"
tshiono
  • 21,248
  • 2
  • 14
  • 22
  • 1
    I downvoted because this answer is a duplicate of https://stackoverflow.com/a/51948654/6770384. – Socowi Feb 09 '19 at 13:42