This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Using a variable to refer to another variable in Bash
(3 answers)
Closed 4 years ago.
Say I have an env variable:
export foo="bar";
declare z="\$foo";
I want to echo the value of "$z", which should be "bar". But if I run:
echo "$z"
I get this literal:
$foo
and if I run this instead:
echo "$(eval "$z")"
I get bar is not a command.
But instead I am looking to just get
"bar"
as the echoed result.
How can I do that?
Related
This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 24 days ago.
The following command (get process name) works for me
ps -q $$ -o comm=
bash
But once I try to assign to variable, it does not work
processname=ps -q $$ -o comm=
-q: command not found
Why is that?
This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 months ago.
I have a variable 'p' which has values 'first second third'
echo $p
first second third
I want to make this output as below
echo $p
first
second
third
This question already has answers here:
Bash expand variable in a variable
(5 answers)
Closed 1 year ago.
I have several variables that have the name format of:
var0_name
var1_name
var2_name
And I want to be able to loop thru them in a manner like this:
for i in {0..2}
do
echo "My variable name = " ${var[i]_name}
done
I have tried several different ways to get this expansion to work but have had no luck so far.
Using the ${!prefix*} expansion in bash:
#!/bin/bash
var0_name=xyz
var1_name=asdf
var2_name=zx
for v in ${!var*}; do
echo "My variable name = $v"
# echo "Variable '$v' has the value of '${!v}'"
done
or equivalently, by replacing the for loop with:
printf 'My variable name = %s\n' ${!var*}
You may also consider reading the Shell Parameter Expansion for detailed information on all forms of parameter expansions in bash, including those that are used in the answer as well.
This question already has answers here:
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 4 years ago.
In a bash script, if I have a variable $FOO which equals "BAR", how would I set the environment variable named BAR equal to "BAZ"? Thanks.
export $(echo "$(echo $FOO)=BAZ") should work. I'm using bash --version = 4.1.2 with success.
export FOO=BAR
export $(echo "$(echo $FOO)=BAZ")
echo $BAR
BAZ
This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 5 years ago.
Suppose I have the following 2 variables:
color=black
round_black_car=IGK47546
Is it possible to print IGK47546 with the variable color?
echo ${round_${color}_car} doesn't work. It gives me bad substitution error.
You should use eval expression combine with backslash to perform the replacement.
$ color=black
$ round_black_car=IGK47546
$ eval echo \${round_${color}_car}
IGK47546