arrays - Bash - Only go next index when new line occurs, instead of white space? -


i'm parsing json response tool called jq. output jq give me list of full names in command line.

i have variable getnames contains json, example:

{     "count": 49,     "user": [{         "username": "jamesbrown",         "name": "james brown",         "id": 1     }, {         "username": "matthewthompson",         "name": "matthew thompson",         "id": 2     }] } 

i pass through jq filter json using following command:

echo $getnames | jq -r .user[].name 

which gives me list this:

james brown    matthew thompson    

i want put each 1 of these entries bash array, enter following commands:

declare -a myarray     myarray=( `echo $getnames | jq -r .user[].name` ) 

however, when try print array using:

printf '%s\n' "${myarray[@]}" 

i following:

james brown matthew thompson 

how ensure new index created after new line , not space? why names being separated?

thanks.

a simple script in bash feed each line of output array myarray.

#!/bin/bash  myarray=() while ifs= read -r line;     [[ $line ]] || break  # break if line empty     myarray+=("$line") done < <(jq -r .user[].name <<< "$getnames")  # print array printf '%s\n' "${myarray[@]}" 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -