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...
Related
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 2 years ago.
I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.
I have a script color.sh
#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1
What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is
./color.sh Apple -> Red, but what I get is some random number which I think is process Id.
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}
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.
This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
#!/bin/sh -f
set proj_dir="OutputDir"
for projname in lib proj1 proj2
do
mv ./scripts/$projname_BYTECODE ./$proj_dir/scripts/$projname
done
A very simple example of what is not working well for me. $projname_BYTECODE is being interpreted as a variable name but _BYTECODE is actually part of the folder name. Suggestions?
Use ${X} instead of $X, so in your example ${projname}_BYTECODE should do the trick. Have a look at this question for more information: When do we need curly braces in variables using 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.