0

I want to export a string with eval in the following way. But I get the string without quotes as indication in the following example

root@linux:~# a="{\"name\":\"any\"}"
root@linux:~# echo $a
{"name":"any"}
root@linux:~# eval "export -- \"b=\"\"$a\"\"\""
root@linux:~# echo $b
{name:any} ------>> expect {"name":"any"}

How to solve this problem?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

2 Answers2

2

This works for me:

$ a='{"name":"any"}'
$ echo $a
{"name":"any"}
$ export b="$a"
$ echo $b
{"name":"any"}

Why do you want to use eval to export a variable? Note, the variable name is interpolated too, so you can do this:

$ foo=bar
$ export $foo=baz
$ echo $bar
baz
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • I m working on open source and I found it in this way. and he is usinng -- in the export command and I don't know why he did it. – MOHAMED Aug 22 '13 at 16:14
  • @MOHAMED: he probably uses "export --" both for clarity (it does indeed show the export more than if it wasn't there) and to make a point that the export is not using any parameters (-f, -p, etc) ? – Olivier Dulac Aug 22 '13 at 16:18
  • +1 for not using `eval` when it isn't necessary (and in this case, there doesn't seem to be any reason to use it at all). – Gordon Davisson Aug 22 '13 at 16:32
1

I'm going to set aside that I don't quite understand why you are doing it like this, but try the following:

bash-3.2$ a="{\"name\":\"any\"}"
bash-3.2$ echo $a
{"name":"any"}
bash-3.2$ eval "export -- b='$a'"
bash-3.2$ echo $b
{"name":"any"}
bash-3.2$
Oli
  • 591
  • 4
  • 10