0

Echo when used with -e option doesn't seem to output/expand value of a variable

Using this way as it's been part of framing json file

Tried with backtick to expand variable value. And also with (( to eval as expression

echo -e '"compVersion:","$compLatestVer",' >> framed.json

eg : with compLatestVer=2.3.4 When I echo it, it just prints

echo -e '"compVersion:","$compLatestVer",'

to file rather than expanded value

I tried with

echo -e '"compVersion:","`$compLatestVer`",'

Also with

echo -e '"compVersion:","((compLatestVer))",'

without luch

echo -e '"compVersion:","2.3.4",'
vinWin
  • 509
  • 1
  • 5
  • 18
  • Nothing inside single quote`'` is expanded. See https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Ashwani Aug 23 '19 at 13:45

1 Answers1

1

Don't use echo. Use printf instead.

printf '"compVersion": "%s"' "$compLatestVer"

However, building up a JSON value piecemeal like this is also wrong; use a tool like jq to generate it for you.

(I adjusted the format string, since it looks like you are trying to output key/value pairs for a JSON object.)

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Many thanks - this helped. Below is final version which addressed problem from OP `printf '"compVersion": "%s,\n"' "$compLatestVer"` – vinWin Aug 26 '19 at 06:56