How do I expand a variable inside single quotes?

Dervin Thunk

I want to run this simple bash script:

  curl https://www.googleapis.com/urlshortener/v1/url?key=MyKey -H \\
  'Content-Type: application/json' -d '{"longUrl": "$1"}'

but bash does not expand the $1 because of the single quotes after -d. 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 $1 inside the single quotes of the json object sent by -d in curl?

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.

Eugene Yarmash

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

curl https://www.googleapis.com/urlshortener/v1/url?key=MyKey -H \\
  'Content-Type: application/json' -d "{\"longUrl\": \"$1\"}"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Expand variable inside single quotes

From Dev

In bash, how do I expand a wildcard while it's inside double quotes?

From Dev

How to echo variable inside single quotes using Bash?

From Dev

How do I include quotes in the command inside the quotes of FOR /F

From Dev

Do not expand string inside variable

From Dev

How do I escape quotes in a string variable?

From Dev

How can I modify text inside single quotes?

From Dev

How can I expand the variable inside this sed expression?

From Dev

How can I add single quotes to a variable in PHP

From Dev

jQueryUI Accordion - how do I expand a single content pane?

From Dev

DelimitMate.vim -- how can I expand return inside triple quotes?

From Dev

How do I expand a snippet, while inside of another snippet?

From Dev

How do I change a variable inside a variable?

From Dev

How do I echo an expression with both single and double quotes?

From Dev

How do I create an alias for a command containing single quotes?

From Dev

How do I get excact single quotes words from the sentence?

From Dev

How to use not like inside single quotes?

From Dev

How to get the string inside single quotes?

From Dev

How to use not like inside single quotes?

From Dev

How to use variables inside single quotes

From Dev

how to remove single quotes inside IN condition cakephp

From Dev

How do I modify this Perl solution so that it will substitute embedded double quotes with single quotes?

From Dev

How do I use a Bash variable (string) containing quotes in a command?

From Dev

How do I include this variable inside this class?

From Dev

How do I use awk's single quotes within another single quote phrase?

From Dev

How to set a variable with both single and double quotes?

From Dev

How can I substitute a text that is inside single quotes on multiple lines using command-line tools?

From Dev

How to remove double quotes inside a variable?

From Dev

How to remove double quotes inside a variable?

Related Related

  1. 1

    Expand variable inside single quotes

  2. 2

    In bash, how do I expand a wildcard while it's inside double quotes?

  3. 3

    How to echo variable inside single quotes using Bash?

  4. 4

    How do I include quotes in the command inside the quotes of FOR /F

  5. 5

    Do not expand string inside variable

  6. 6

    How do I escape quotes in a string variable?

  7. 7

    How can I modify text inside single quotes?

  8. 8

    How can I expand the variable inside this sed expression?

  9. 9

    How can I add single quotes to a variable in PHP

  10. 10

    jQueryUI Accordion - how do I expand a single content pane?

  11. 11

    DelimitMate.vim -- how can I expand return inside triple quotes?

  12. 12

    How do I expand a snippet, while inside of another snippet?

  13. 13

    How do I change a variable inside a variable?

  14. 14

    How do I echo an expression with both single and double quotes?

  15. 15

    How do I create an alias for a command containing single quotes?

  16. 16

    How do I get excact single quotes words from the sentence?

  17. 17

    How to use not like inside single quotes?

  18. 18

    How to get the string inside single quotes?

  19. 19

    How to use not like inside single quotes?

  20. 20

    How to use variables inside single quotes

  21. 21

    how to remove single quotes inside IN condition cakephp

  22. 22

    How do I modify this Perl solution so that it will substitute embedded double quotes with single quotes?

  23. 23

    How do I use a Bash variable (string) containing quotes in a command?

  24. 24

    How do I include this variable inside this class?

  25. 25

    How do I use awk's single quotes within another single quote phrase?

  26. 26

    How to set a variable with both single and double quotes?

  27. 27

    How can I substitute a text that is inside single quotes on multiple lines using command-line tools?

  28. 28

    How to remove double quotes inside a variable?

  29. 29

    How to remove double quotes inside a variable?

HotTag

Archive