简体   繁体   中英

How do I assign something to a JQ variable I've already set in JQ?

I'm setting some variables before I run a JQ that I want to set some fields to. Like so:

jq --arg user $SOME_USER '.something.user|="$user Did a thing"'

But when I pipe this to a file or look at stdout it seems to pick up the variable but not replace it in the string in the JQ script.

jq does do variable interpolation; the syntax is just a little different from other languages. You have to explicitly request interpolation with \\(...) .

jq --arg user "$SOME_USER" '.something.user|="\($user) did a thing"'

I was dumb as a stack of bricks. JQ doesn't render variables in strings in the same way as some languages will do. In my case, I could simply have kept the variable out of the string and written: $user + "...".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM