Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell Scripting: Get string inside quotations

Posted on 2013-11-08
3
Medium Priority
?
204 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 20

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 20

Accepted Solution

by:
simon3270 earned 2000 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 20

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

972 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