Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq not writing value of variable but the actual text of the variable name

I’m scripting in bash to edit a json template to replace some field values with arguments to my script, and trying to use jq to do the editing. My code is not replacing the –arg with the value of the argument, but the literal text of the argument name.

My template contains:

{
"name":""
}

My jq code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jq --arg ad "192.168.5.5" -r '.name = "Addr $ad"' address.tmpl

This outputs:

{
    "name": "Addr $ad"
}

Or, if I remove the double-quotes

jq --arg ad "192.168.5.5" -r '.name = Addr $ad' address.tmpl

I get

jq: error: syntax error, unexpected '$', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.name = Addr $ad
jq: 1 compile error

According to all that I have read, this should work. What am I doing wrong/how do I fix this????

OS = debian 10

>Solution :

jq --arg ad "192.168.5.5" -r '.name = "Addr " + $ad' address.tmpl

It must be hardquoted for the shell (to stop shell expansion), and not quoted for jq. Edit: unless it is escaped, like \($ad). Use whichever you find more readable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading