Printing a variable value with the name of another variable [duplicate] - bash

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

Related

Bash initialize variable to a string that contains quotes and backslahes [duplicate]

This question already has answers here:
Setting an argument with bash [duplicate]
(2 answers)
Closed 5 months ago.
I have a string containing quotes and backslahes:
-options \'{"version": "http"}\'
I would like to initialize a variable PARAM with this string.
How this can be done in bash?
I thought of adding it to an array: PARAMS=(-options \'{"version": "http"}\')
but the output I am getting is: -options '{version: http}' i.e. without the slashes.
Expected output: -options \'{"version": "http"}\'
Can someone please suggest?
This looks ok to me.
test="-client-options \\'{\"quic-version\": \"h3\"}\\'"
echo "$test"
t2=("$test" "etc")
echo ${t2[#]}
Escape every inner " and double escape for a persisting escape

Bash variable name expansion in a loop [duplicate]

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.

shell script count consecutive spaces [duplicate]

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 2 years ago.
I've got a string h="aaaa bbb" which contains 2 spaces between aaaa and bbb,and I want to count the number of spaces in it.However,when I try
echo $h|grep -o ' '|wc -l
it shows 1 instead of the desired two.
Is there any way not to treat consecutive spaces in string as one?

Sed is taking the file name instead of empty value [duplicate]

This question already has answers here:
Pass empty variable in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have a replace function like
function replace()
{
sed -i "s#$1#$2#g" $3
}
I am calling the function with these parameters
replace MY_IP $MY_IP /usr/xxx.sh
where $MY_IP is a empty value
so sed is giving as sed -i s#MY_IP#/usr/xxx.sh#g
sed no input files
It is not taking the empty value. How to solve this?
Try quoting $MY_IP.
replace MY_IP "$MY_IP" /usr/xxx.sh
Without the quotes, bash will just skip that argument.

evaluate but not execute environment variable [duplicate]

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?

Resources