Dervin Thunk Dervin Thunk - 4 months ago 19x
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.


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

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