简体   繁体   English

如何将某些内容分配给我已经在 J​​Q 中设置的 JQ 变量?

[英]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.在运行我想设置一些字段的 JQ 之前,我正在设置一些变量。 Like so:像这样:

jq --arg user $SOME_USER '.something.user|="$user Did a thing"' jq --arg user $SOME_USER '.something.user|="$user 做了一件事"'

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.但是,当我将其通过管道传输到文件或查看 stdout 时,它似乎获取了该变量,但并未在 JQ 脚本的字符串中替换它。

jq does do variable interpolation; jq确实做了变量插值; 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. JQ 不像某些语言那样在字符串中呈现变量。 In my case, I could simply have kept the variable out of the string and written: $user + "...".就我而言,我可以简单地将变量保留在字符串之外并写入:$user + "..."。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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