0

I would like to rename my package.json "name" value like this:

PROJECT_NAME="MyProject"
jq '.name = "$PROJECT_NAME"' package.json > tmp.$$.json && mv tmp.$$.json package.json

But it doesn't work. Without the variable works as expected:

jq '.name = "MyProject"' package.json > tmp.$$.json && mv tmp.$$.json package.json

What I am doing wrong?

miqrc
  • 1,964
  • 2
  • 18
  • 24
  • 4
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jan 10 '19 at 20:16
  • 1
    ...though it's bad form to use variables to generate code (and jq queries *are code*) regardless; just switching to double quotes fixes the immediate bug but introduces new issues with unusual project names. Using `jq --arg` to pass a variable into `jq`'s context so a constant variable lookup can refer to it is very much the Right Thing. – Charles Duffy Jan 10 '19 at 20:44
  • 1
    (Then again, if we cared about the right thing, we wouldn't have all-caps names for user-defined shell variables either; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that shell and environment variables share a namespace -- setting a shell variable overwrites any like-named environment variable, so naming conventions necessarily apply to both). – Charles Duffy Jan 10 '19 at 20:48
  • Okay, thank you for the advises! – miqrc Jan 10 '19 at 21:38

0 Answers0