如何将bash变量作为jq的键传递? [英] How to pass bash variable as a key to jq?
本文介绍了如何将bash变量作为jq的键传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是示例json
{
"app": "K8s",
"version": "1.8",
"date": "2018-10-10"
}
为了获得app的价值,我可以在jq中这样做
In order to get the value of app, I can do this in jq as
jq '.app'
但是我想要的是,我想将密钥作为bash变量传递给jq,即
But what I want is, I want to pass the key to jq as a bash variable, i.e
bash_var="app"
jq '."${bash_var}"'
我得到的输出为 null 而不是值.实现此目的的正确语法是什么?
I'm getting the output as null instead of the value. What is the correct syntax to achieve this?
推荐答案
首先,您需要使用--arg
标志将bash
变量移植到jq
的上下文中,并在[..]
First, you need to port the bash
variable into jq
's context usign the --arg
flag and access it inside the [..]
jq --arg keyvar "$bash_var" '.[$keyvar]' json
这篇关于如何将bash变量作为jq的键传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文