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\"
Related
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
This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
I want to create a shell script function that runs this:
docker inspect 5914ba2819d6 | jq '.[0].NetworkSettings.Networks["foo"].IPAddress'
I'm trying to get the function to run like:
getcontainerip 5914ba2819d6 foo
I've edited .bash_profile to have many permutations like this:
getcontainerip() {
docker inspect "$1" | jq '.[0].NetworkSettings.Networks["$2"].IPAddress'
}
No matter what, the literal double quotes never get added surrounding the second parameter. The output will either be blank (because the double quotes aren't there), or a syntax error:
jq: error: syntax error, unexpected '$' (Unix shell quoting issues?)
How do I get the literal double quotes to surround the second parameter? Thanks.
Try this:
getcontainerip() {
docker inspect "$1" | jq '.[0].NetworkSettings.Networks["'$2'"].IPAddress'
}
The outer '' have the effect that $2 is not replaced with the second parameter but rather passed literally to jq. That's why you need to close the ''s temporarily to let the shell expand $2.
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?
This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
Problem
How do we use variables in a sed edit string?
Example
The file statement.txt is the sentence
I like my pet bird.
Given a variable ${myPet}, how can we use sed to replace bird with the value in ${myPet}?
What doesn't work
sed -ie 's/bird/${myPet}/g' statement.txt
The result is
I like my pet ${myPet}.
' single quotes don't expand value of a shell variable so you need to use " double quotes here.
myPet="your_value"
sed -ie "s/bird/${myPet}/g" statement.txt
This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 4 years ago.
I want to escape double quotes in Bash. I followed the following approach:
#!/bin/bash
this is a \"number\"!
But is there another way?
You can enclose the double quotes in single quotes:
echo '"'hola'"'
Or alternatively the whole text, including the double quotes:
echo '"hola"'
With GNU Bash:
echo -e "this is a \x22number\x22"
Output:
this is a "number"