'Expand variable inside curly braces, inside single quotes, inside double quotes

I'm trying to write a bash script that takes a variable and populates it inside a somewhat complex string and I can't figure out how to get it to work.

I have the following bash code..

PWD="foobar"
curl -XPOST "localhost/api/user/bob" -H 'Content-Type: application/json' -d '{"password" : "${PWD}"}

what I want to have happen is obviously this:

curl -XPOST "localhost/api/user/bob" -H 'Content-Type: application/json' -d '{"password" : "foobar"}

but none of the iterations and expansion "tricks" I know seem to work because of the braces and the single and double quotes.

I've tried

$PWD
${PWD} 

Both to no avail.



Solution 1:[1]

You need to use double quotes to allow the $PWD to expand. (The braces are irrelevant here.)

-d "{\"password": \"$password\"}"

Better yet, though, use something like jq to generate JSON so that you can be sure everything is quoted correctly without shell interference.

-d "$(jq -n --arg p "$password" '{password: $p}')"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 glenn jackman