Ganesh
Ganesh

Reputation: 307

Manipulating Single quote in string

I am running below shell script

     var1="'"
    json_variable=$var1{"id":158,"name":"stackoverflow"}$var1
    echo $json_variable

I am getting below output

'{id:158,name:stackoverflow}'

How can I get output in below format

'{"id":158,"name":"stackoverflow"}'

Thanks,

Upvotes: 0

Views: 33

Answers (2)

chepner
chepner

Reputation: 532398

I'm not sure why you need single quotes specifically in the output, but your root problem is that your assignment never adds the double quotes in the first place. First, quote the double quotes properly, using

json_variable='{"id":158,"name":"stackoverflow"}'

Then, add the single quotes to the echo command:

echo "'$json_variable'"

The same trick can be used to add single quotes to the value without worrying about complicated quoting schemes.

json_variable="'$json_variable'"

Upvotes: 0

William Pursell
William Pursell

Reputation: 212654

I think you are looking for:

json_variable="'{\"id\":158,\"name\":\"stackoverflow\"}'"

or perhaps you want

json_variable="'"'{"id":158,"name":"stackoverflow"}'"'"

or

json_variable=\''{"id":158,"name":"stackoverflow"}'\'

or

read json_variable << \EOF
> '{"id":158,"name":"stackoverflow"}'
> EOF

Upvotes: 3

Related Questions