Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Shell Scripting: Get string inside quotations

x=1; \
y=2; \
echo x: $x - '{"y": "$y"}'

Open in new window

I would expect this result:
x: 1 - {"y": "2"}

Open in new window

but instead I get this results:
x: 1 - {"y": "$y"}

Open in new window

0
hankknight
Asked:
hankknight
  • 3
1 Solution
 
simon3270Commented:
It's because the double quotes are nested within single quotes.  try:
    echo x: $x - '{"y": '"$y"'}'
(that's a single quote between the space and the double quote before the $y, and one between the double-quote after $y and the curly brace)
0
 
simon3270Commented:
As you can see quotes are hard!

I meant:
    echo x: $x - '{"y": "'$y'"}'
where the extra quotes are between the double-quote and the $y, and between the $y and its trailing double-quote
0
 
simon3270Commented:
Essentially, you put single quotes round text that you don't want changed at all, then double quotes (or no quotes at all) round bits where you want variable substitution to take place.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now