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.

noober
  • 1,427
  • 3
  • 23
  • 36

1 Answers1

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\" }}}})";

signjing
  • 98
  • 4