0

So, I'm working on a couple of functions in my bash script. I have one that works, but my 2nd one includes a number of values enclosed in double quotes "", thus breaking my variable substitutions. I've tried surrounding those values with a single tick, but that doesn't work in some cases.

For example:

# Functions
unset_versions ()
{
   host='127.0.0.1'
   db='mydev'
   _account='"foo"'
   _profile='"bar"'
   _mongo=$(which mongo);
   exp="db.profile_versions_20170420.update({account:${_account}, profile:${_profile}, version:${_version}},{ \$unset : { labels : true }});";
   ${_mongo} ${host}/${db} --eval "$exp"
}

However this won't work:

update_versions ()
{
   host='127.0.0.1'
   db='mydev'
   _account='"foo"'
   _profile='"bar"'
   _mongo=$(which mongo);
   exp="db.profile_versions_20170420.update({account:${_account}, profile:${_profile}, version:${_version}},{ \$set : { labels : { "1" : { "name" : "data layer patch", "color" : "regal-rose" }, "2" : { "name" : "Global Functions", "color" : "melon-mambo" }}}});
   ${_mongo} ${host}/${db} --eval "$exp"
}

I know this looks ugly. If there's a better way please provide some suggestions. I also tried doing exp=$(....), but that doesn't seem to work either.

2
  • 1
    You may want to take a look at this post: stackoverflow.com/questions/6697753/… Commented Apr 24, 2017 at 4:54
  • this is a good reference. thank you. Commented Apr 24, 2017 at 16:37

1 Answer 1

1

You should escape '"' like this:

exp="db.profile_versions_20170420.update({account:${_account},profile:${_profile}, version:${_version}},{ \$set : { labels : { \"1\" : { \"name\" : "datalayer patch\", \"color\" : \"regal-rose\" }, \"2\" : { \"name\" : "Global Functions\", \"color\" : \"melon-mambo\" }}}})";

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.