I’m trying to write a bash script that add an item to an array in my bsconfig.json file using jq. I can’t figure out the syntax for doing so.
This doesn’t work:
function addbsdep() {
newDep="$@"
jq '.["bs-dependencies"] += ["$newDep"]' bsconfig.json | sponge bsconfig.json
}
In terminal you would run addbsdep reason-react. The above adds $newDep rather than reason-react to the json file.
What is the correct syntax?
Update - Solution:
function addbsdep() {
newDep="$@"
echo $newDep
jq --arg v "$newDep" '.["bs-dependencies"] += [$v]' bsconfig.json | sponge bsconfig.json
}