I'm having so much troubles trying to create a string that is the result of the concatenation of an array with a bit of addition every time it goes through the loop
# HERE IS THE VALUE OF CHANGELOG.JSON
# [
# 13,
# 14,
# 22
# ]
readarray -t lines <<<"$(cat $1/changelog.json | jq '.[]')"
echo "check if my array is filled"
echo ${lines[0]}
echo ${lines[1]}
echo ${lines[2]}
echo "my array is correctly filled.."
for i in ${!lines[@]}
do
var2="$var2 | jq '.[$((($i+1)))]=${lines[$i]}'"
echo "my var2 during loop: $var2"
done
echo "my var2 after loop $var2"
For some reasons, this script give me this as output
$ /bin/bash add-changelog.sh Blupods-front-v3/
check if my array is filled
13
14
22
my array is correctly filled..
'y var2 during loop: | jq '.[1]=13
' | jq '.[2]=14loop: | jq '.[1]=13
' | jq '.[3]=22'oop: | jq '.[1]=13
' | jq '.[3]=22'op | jq '.[1]=13
do you guys have any idea of how may i make it work ? i've already went through a lot of the solutions on stackoverflow and none of them worked (using += for concatenation) I've heard on other questions this is due to the fact that for/while loop create a subshell but even in the loop the variable doesn't get memorized.. also I don't have any pipe except for the read array at first.
EDIT: Here I add my expected result, maybe it will help you guys help me I am not trying to execute this command right away, i'm just trying to concat a string in a for loop the expected result should be this
echo $var2
| jq '.[1]=13' | jq '.[2]=14 | jq '.[3]=22'
var2="$var2 | jq '.[$((($i+1)))]=${lines[$i]}'"supposed to do? What is the initial value of var2? That assignment would not execute the pipeline if that is what you are expecting. It will simply assign the whole string to var2, and not the result of the pipeline execution. You need$(...)to execute the pipeline.$var2? There is likely a solution that doesn't require dynamically generating a pipeline of multiple calls tojq.jq --slurpfile cl changelog.json '.[:3] = $cl[]' original.jsonmay do what you want.