0

I know that bash supports indirect variable references using the ! operator:

$ ABC=123
$ DEF=ABC
$ echo ${!DEF}
123

and that's nice. But - it doesn't seem work when I want to set the value of a variable, whose name I get from another variable:

$ ABC=123
$ DEF=ABC
$ !DEF=1234
-bash: !DEF=1234: event not found
$ ${!DEF}=1234
-bash: 123=1234: command not found

Other than by using eval - how can I set the value of a variable whose name is the value of another variable?

einpoklum
  • 102,731
  • 48
  • 279
  • 553
  • You can create a `nameref`. See [man 1 bash - nameref](http://man7.org/linux/man-pages/man1/bash.1.html) I think that was a bash 4.x addition. – David C. Rankin May 24 '20 at 08:21
  • @DavidC.Rankin: Is this supported with any bash version, or just newer ones? – einpoklum May 24 '20 at 08:23
  • It will work with the newer 4.x on. The nameref was added to get around limitations in the indirect reference. It's still not perfect. But it will work here, e.g. `abc=123; declare -n ref=abc; ref=1234; echo $abc` gives you `1234` – David C. Rankin May 24 '20 at 08:26
  • Since bash 4.0 was release in 2009 -- that should cover any current OS -- unless it is really really old. – David C. Rankin May 24 '20 at 08:30
  • `declare -n `was introduced with version 4.3. [Source](http://mywiki.wooledge.org/BashFAQ/061) – Cyrus May 24 '20 at 08:32
  • That moves things up 5-years to 2014. That is in still running old server territory (it would be embarrassing to note, I just retired a box put in service in 2002 last year -- an old Abit KT7-raid with Tbird processor - caps finally gave out `:)`. – David C. Rankin May 24 '20 at 08:33
  • This is a duplicate of [Indirect variable assignment in bash](https://stackoverflow.com/questions/9938649/indirect-variable-assignment-in-bash). See [chepner's answer](https://stackoverflow.com/a/11460242/10248678). – oguz ismail May 24 '20 at 09:03
  • The problem with [chepner's answer](https://stackoverflow.com/a/11460242/10248678) with the question above is `123` is not a valid identifier for the indirect variable when using `declare` or `typeset` alone. (can't have an identifier begin with a number). That is what `nameref` avoids. – David C. Rankin May 24 '20 at 10:10
  • @David Didn't understand what you mean but the answer also includes a solution using namerefs. – oguz ismail May 25 '20 at 03:21
  • Yes, but the declare and typeset would leave the indirect reference in the example starting with a number which would make it an invalid variable name. It appears there is just an extra `'$'` included. For example if `val=123` (as in this question) then `typeset $var=$val` results in the indirect reference in `var` being `$123`. – David C. Rankin May 25 '20 at 04:27

1 Answers1

0

In respect to your question(s)

... I want to set the value of a variable, whose name I get from another variable ... How can I set the value of a variable whose name is the value of another variable?

you may find the answer in Dynamic variable names in Bash. There are further options like read, printf or declare mentioned.

Further information you may find under

Or Why should eval be avoided in Bash, and what should I use instead? about How to make eval safe?.

U880D
  • 3,600
  • 5
  • 18
  • 30