jq: error: $i is not defined at <top-level>

Viewed 54

Im doing a bash script where I use a for loop in order to get some information using a AWS command, the output of the command is a list in Json so, i'm using jq for filtering the response. As I'm expecting to pass the output of the first command to another i need to send each element of the list individually and here is where I'm in troubles.

for i in {0..1}
do
aws ecs list-tasks --cluster $1 --profile $2 | jq '.taskArns[$i]'
done

It doesn't matter how I set the for, if I set it as for i in {0..1} or for i in "${tasks[@]}" when I run the script it give back the same error, however if I put the index as taskArns[0] or taskArns[1] the script works.

Thanks in Advice!

1 Answers

You never set the jq var $i before using it. You could do so as follows:

jq --arg i "$i" '.taskArns[ $i | tonumber ]'

You might want to move the loop into jq to avoid calling aws twice.

aws ecs list-tasks --cluster $1 --profile $2 | jq '.taskArns[0:2][]'
Related