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.
Related
This question already has answers here:
Passing bash variable to jq
(10 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
I'm attempting to parse the output of a script that takes a parameter and for each parameter pulls some json. I'm parsing with jq.
On the command line for one run of the script a command like this one works:
./script_that_produces_json --intput=value | jq '.[] | select(.name=="objecName") | .fieldIWant
I need to run this for a variety of parameters, which I am attempting to read from an array. This is what my code looks like now:
for entry in "${array[#]}"
do
output=$(./script_that_produces_json --input=$entry | jq '.[] | select(.name=="$entry") | .fieldIWant)'
print($output)
done
This yields the variable substituted, but there are no quotes around it, which fails when passed to jq. I've been hacking around this with various combinations of quotes with no success.
How do I quote the variable/string correctly such that both the variable is replaced and the quotes are passed?
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
USER_UID=$1
echo 'generate_token("$USER_UID")'
I want output like
generate_token("1234567")
i tried multiple ways but didn't worked. it just print same line without value generate_jwt("$USER_UID")
When you use single quotes, it causes the shell to preserve the literal value of each character within the quotes. This means the $ will be treated as a literal $ character.
You should use double quotes:
USER_UID="$1"
echo "generate_token(\"$USER_UID\")"
From the bash man page, under the Quoting section:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.
For POSIX details on quoting, see here.
Example in an interactive shell:
$ USER_UID='foo'
$ echo "generate_token(\"$USER_UID\")"
generate_token("foo")
This will also work if USER_UID contains spaces:
$ USER_UID='var with spaces'
$ echo "generate_token(\"$USER_UID\")"
generate_token("var with spaces")
This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 2 years ago.
i have a file test.txt with data init=6'b000000; and i want to replace it with init=6'b110111; by using vim script in a bash file. I'm getting error
I'm using following command:
vim -c '%s/init=6'b000000;/init=6'b110111;/g | write | quit' test.txt
this works perfectly in vim, not in bash .
This has nothing to do with Vim, you cannot embed a single quote inside a pair of other single quotes. The shell parses the command line arguments before passing them to the invoked command and it just cannot deal with the inner single quote in the way you have defined.
The literal single quote inside need to be preserved before passing to the command. So use double quote and escape the inner quote
vim -c "%s/init=6\'b000000;/init=6\'b110111;/g | write | quit" file
or use the single quote, but include a multi-level double quote inside
vim -c '%s/init=6'"\'"'b000000;/init=6'"\'"'b110111;/g | write | quit' file
This question already has answers here:
How can I preserve quotes in printing a bash script's arguments
(7 answers)
Closed 4 years ago.
I have a script that logs the user argument list. This list is later processed by getopt.
If the script is started like:
./script.sh -a 'this is a sentence' -b 1
... and then I save "$#", I get:
-a this is a sentence -b 1
... without the single quotes. I think (because of the way Bash treats quotes) these are removed and are not available to the script.
For logging accuracy, I'd like to include the quotes too.
Can the original argument list be obtained without needing to quote-the-quotes?
No, there is no way to obtain the command line from before the shell performed whitespace tokenization, wildcard expansion, and quote removal on it.
If you want to pass in literal quotes, try
./script.sh '"-a"' '"this is a sentence"' '"-b"' '"1"'
Notice also how your original command line could have been written
'./script.sh' '-a' 'this is a sentence' '-b' '1'
This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 4 years ago.
I have a very simple bash script with three commands.
The first command strips the first word off of the last git commit, the second command attempts to make a POST call to an api endpoint, with that same variable as part of the call, and the third command just prints that variable, to ensure it was working properly. See the code below
SOMETHING=$(git log -1 --pretty=%B | head -n1 | sed -e 's/\s.*$//' | cut -d ' ' -f1)
curl -X POST \
http://www.someurl.com/ \
-H 'Cache-Control: no-cache' \
-d '{"item":"$SOMETHING"}'
echo "variable was $SOMETHING"
When I run that bash script, I get a response from the service saying that "item was not set properly" in XML, however it does correctly echo the correct variable. So I know that first line is working. If I copy that curl command and paste it into bash, replacing $SOMETHING with the actual value, it works fine.
Single quotes do not expand the $variables inside them.
Try
'{"item":"'"$SOMETHING"'"}'
instead. Brief explanation:
'{"item":"' is a string delimited by single quotes that contains double quotes
"$SOMETHING" is a string delimited by double quotes, that expands the variable $SOMETHING
'"}' is again a ''-delimited string that contains double quotes
Simply writing those strings in a row without gaps is string concatenation
In this way, you get your variable expansion, but don't have to insert any backslashes to escape the double quotes.