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

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

Related

How to write a variable in a script from another script without sending the value of the variable? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
Basically, I want to send a variable as $1 in another script without the value it has saved.
#!/bin/bash
echo -e "#!/bin/bash\ncp ~/src/$1" > ~/asset/newfile.sh
So, that in the file newfile.sh it is written:
#!/bin/bash
cp ~/src/$1
You can escape the dollar sign with a backslash:
echo -e "#!/bin/bash\ncp ~/src/\$1"
Or, switch to single quotes:
echo -e '#!/bin/bash\ncp ~/src/$1'

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.

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

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

Adding quotes to a single variable [duplicate]

This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 5 years ago.
I want to add double quotes around my variable string, any advice on how I adapt my code? Simple example below
#!/bin/bash
variable="variable"
echo $variable
I'd like to see the output as
"variable"
Escape the quotes:
echo \"$variable\"

Print a variable containing spaces in bash [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I have this string:
string="I love spaces"
I would like to print that so that the spaces would remain. echo $string doesn't seem to print the spaces as well.
Desired Output:
I love spaces
Nevermind me fellas. echo "$string" :/

Resources