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
Post a Comment