Dervin Thunk Dervin Thunk - 1 year ago 128
Bash Question

How do I expand a variable inside single quotes?

I want to run this simple bash script:

curl -H \\
'Content-Type: application/json' -d '{"longUrl": "$1"}'

but bash does not expand the
because of the single quotes after
. Google returns, expectedly, and error:

"error": {
"errors": [
"domain": "global",
"reason": "invalid",
"message": "Invalid Value",
"locationType": "parameter",
"location": "resource.longUrl"
"code": 400,
"message": "Invalid Value"

How do I expand the
inside the single quotes of the json object sent by

This question does seem to be a duplicate, but I cannot figure out the answer from the other ones. I'm providing the script in the hopes it will be useful for someone else as a contribution.

Answer Source

You could just use double quotes, escaping the ones inside the JSON string:

curl -H \\
  'Content-Type: application/json' -d "{\"longUrl\": \"$1\"}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download