0

I have the following data in a file -

{"a" : "10", "b" : "20", "c" : "30"}

When I read values in this variable -

eval "_value_=\"`cat hello1`\""

echo $_value_ equals {a : 10, b : 20, c : 30}

I can't read this using jq -r. Is there any way I can read double quotes in value as well.

2 Answers2

0

Not certain what you mean, but it sounds like your file hello1 contains:

{"a" : "10", "b" : "20", "c" : "30"}

and you want to get that into a variable called _value_. If that is the case, you just do:

_value_=$(< hello1)

Then you can do:

echo "$_value_"
{"a" : "10", "b" : "20", "c" : "30"}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Here's a portable POSIX solution (no bashisms):

Contents of hello1 file:

{"a" : "10", "b" : "20", "c" : "30"}

POSIX shell code:

_value_="$(cat "hello1")"
echo "$_value_"

Output:

{"a" : "10", "b" : "20", "c" : "30"}

I'm not sure why you want to use eval. That command is very dangerous, especially if you're not completely sure of the contents of the file you're reading. Consider using GNU envsubst if you merely need variables interpreted (this seems unlikely given your JSON input):

_value_="$(envsubst < "hello1")"
echo "$_value_"
Adam Katz
  • 14,455
  • 5
  • 68
  • 83