Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq result iteration in bash

Hi i have this jq output from a CLI command

["test-KEY","test-PASSWORD"]

Assigned that output to a bash variable in this manner

secret_list=`mycli | jq`

I want to iterate though that output in bash & perform another CLI command on each item

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so far i tried this block & is not coming desired command execution

echo "secrets_list: "$secrets_list

length=${#secrets_list[@]}
echo "Number of secrets $length"

for secret in "${secrets_list[@]}"
do
      echo "$secret"
done

Output is

secrets_list: ["test-KEY","test-PASSWORD"]
Number of secrets 1
["test-KEY","test-PASSWORD"]

my expectation is to iterate through that array. I am not sure what i am doing wrong. Thanks in advance for help

>Solution :

Bash arrays are not JSON arrays and JSON arrays are not bash arrays. If you none of your values contains new lines, you could use read in a loop:

mycli | jq -r '.[]' | while read -r secret; do
  echo "$secret"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading