0

How can I access an environment variable from another? I have the following in my shell

#!/usr/bin/env bash

set -x

export A_version=1.0.0

component=A

echo ${${component}_version}}

the bash script after the run gives me

temp.sh: line 9: ${${component}_version}}: bad substitution
David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • that works if you want to simply echo the content of ${component}_version. now how can I assign the value to another variable. something like b=`eval "echo \$${component}_version"` ? would that work? – user4380125 Dec 08 '17 at 00:00
  • actually it looks like version=`eval "echo \\$${component}_version"` works. – user4380125 Dec 08 '17 at 00:07
  • Works only if you trust the variable's contents not to be malicious. If `component='(rm -rf ~)'`, then you're in a very bad place when you run that. Other (non-`eval`-based) parameter expansion syntax doesn't carry those risks. See also [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) re: why `eval` is generally discouraged. – Charles Duffy Dec 08 '17 at 00:10
  • I've looked at the myriad resources you linked to but I can't actually find a working code example for solving the OP's problem that doesn't use `eval`. How about you post an answer here with one? – David Grayson Dec 08 '17 at 17:29
  • The alleged duplicates to the this question do not address how one would add a suffix like `_version` to the variable name before accessing it, so this should be reponed. – David Grayson Dec 08 '17 at 17:38
  • @DavidGrayson, ...I added a duplicate that specifically speaks to suffixing variables: [Reference an appended variable?](https://stackoverflow.com/questions/37192093/reference-an-appended-variable) -- note that the author of the accepted answer suggests it not be used; [the answer by @chepner](https://stackoverflow.com/a/37192243/14122) is the best-practice approach. – Charles Duffy Dec 08 '17 at 17:43

1 Answers1

1

You can use eval to do this. Here is a working version of your script that prints 1.0.0:

export A_version=1.0.0
component=A
eval "echo \$${component}_version"

For more information, see this page:

http://tldp.org/LDP/abs/html/ivr.html

Update: A safer way to do the same thing in Bash would be:

export A_version=1.0.0
component=A
var=${component}_version; echo "${!var}"

Note that you have to run this script with bash, not sh.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Please don't encourage `eval` for indirect evaluation, *or* encourage the ABS as a reference -- it's very much the W3Schools of bash; half of what we do in the freenode #bash channel is help people unlearn bad habits they picked up there. [BashFAQ #6](http://mywiki.wooledge.org/BashFAQ/006) is a reference on indirect variable use that's more actively focused on best practices. The bash-hackers' wiki also [covers indirection in its page on parameter expansion](http://wiki.bash-hackers.org/syntax/pe#indirection). – Charles Duffy Dec 07 '17 at 23:47
  • Searching for "indirect expansion" in [the relevant manual page](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) also works. – Charles Duffy Dec 07 '17 at 23:51
  • None of your suggestions seem to solve the OP's problem, how about you post your own answer? – David Grayson Dec 08 '17 at 17:31
  • The linked duplicates solve the OP's problem -- see the list at the top of the question. Accordingly, this question has been closed, and is not open to new answers. – Charles Duffy Dec 08 '17 at 17:33
  • How about posting a comment with the code? What exactly would you put on line 3 of my script? – David Grayson Dec 08 '17 at 17:34
  • `var=${component}_version; echo "${!var}"` – Charles Duffy Dec 08 '17 at 17:34
  • ...that technique is literally described in at least one answer to every single question I linked. – Charles Duffy Dec 08 '17 at 17:36