Shell variable inside variable [duplicate] - bash

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
What is wrong in this substitution.
$ m_d_ver=0.2
$ m=mod
$ d=dom
$ echo ${$m_$d_ver}
-bash: ${$m_$d_ver}: bad substitution
Thanks,

What you're trying to do is an indirect variable lookup. The syntax for that is ${!namevar}, where namevar is a variable that contains the name you actually want to evaluate. Thus:
mod_dom_ver=0.2
m=mod
d=dom
var=${m}_${d}_ver
echo "${!var}"
See BashFAQ #006.

Related

how to call a string as a variable in shell script? [duplicate]

This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 1 year ago.
I have a problem that I will not know the name of the variable and the name of the variable will be stored in an array that I have,
the problem here how can I call it, I tried some in the cli to see:
$ hello=sup
$ hi=hello
$ echo $`echo $hi`
$hello
as you see it prints "$hello" instead of "sup" that I want
To dereference a variable you have to use the $ so the second assignment isn't working in your code sample.
>hello=sup
>hi=$hello
>echo $hi
sup
See https://tldp.org/LDP/abs/html/ivr.html for more info on indirect variable references.
Just use the following in bash:
echo ${!hi}
or eval in other shells
eval "echo \$$hi"

Bash -Bad substitution error when evaluating two env variables [duplicate]

This question already has answers here:
creating environment variable with user-defined name - indirect variable expansion
(1 answer)
Dynamic variable names in Bash
(19 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 3 years ago.
This is my script:
#!/bin/bash
AREA="DEV"
DEV_AREA_USER="DevAdmin"
TEST_AREA_USER="TestAdmin"
TEST=${$AREA_AREA_USER}
echo ${TEST}
Result: Bad substitution error. Expected result: DevAdmin
How to fix this? I do not want to create a new variable because there are 75 such variables and 75 files to edit. The lesser the better...

Bash expand variable in an another variable [duplicate]

This question already has answers here:
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
What does "${!var}" mean in shell script? [duplicate]
(1 answer)
Closed 4 years ago.
I have the following variables
my_country_code="green"
x="country"
echo ${my_$x_code}
bash: ${my_$x_code}: bad substitution
echo should print green as output, but unable to find any technique which will give the correct output
my_x_code="my_${x}_code"
echo ${!my_x_code}

bash bad substitution - access an environment variable from another [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Reference an appended variable?
(3 answers)
Closed 5 years ago.
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
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.

Shell script variable reflection [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.

Resources