tot*_*tot 1 arrays bash shell json jq
我有脚本
#!/bin/bash
ARR=("a" "b")
collection_init_msg=$( jq -n --arg arr $ARR '{arr: [$arr]}')
echo some command "$collection_init_msg"
Run Code Online (Sandbox Code Playgroud)
应该转换 ARR 并将其打印为 JSON 数组。
当前结果
一些命令 { "arr": [ "a" ] }
我想要的是:
某些命令 { "arr": [ "a", "b" ] }
#!/bin/bash
ARR=("a" "b")
collection_init_msg=$( jq -nc '{arr: $ARGS.positional}' --args "${ARR[@]}" )
echo "$collection_init_msg"
Run Code Online (Sandbox Code Playgroud)
回答评论中的补充问题:可以做得比以下更糟糕:
jq -n --argjson a1 $(jq -nc '$ARGS.positional' --args "${A1[@]}") '
{$a1, a2: $ARGS.positional}' --args "${A2[@]}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |