Solved

Shell Scripting: Get string inside quotations

Posted on 2013-11-08
3
192 Views
Last Modified: 2013-11-08
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
Comment
Question by:hankknight
  • 3
3 Comments
 
LVL 19

Expert Comment

by:simon3270
ID: 39633450
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
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 39633465
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
 
LVL 19

Expert Comment

by:simon3270
ID: 39633469
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now