3

"$emails" has the array of values, i want to parse the values from it, To do it, i am using the jq. if i do below command

echo "$emails" | ./jq '.total_rows'

i could get the value i.e 4, i want to store the returned results into some variable,

total_rows="$emails" | ./jq '.total_rows'

but total_rows has no value.

echo $total_rows

How do store the returned result into variable?

Sivailango
  • 544
  • 1
  • 6
  • 15

1 Answers1

10

You have to use the right quotation, like this:

total_rows=`echo "$emails" | ./jq '.total_rows'`

The `` will execute the command and give total_rows the value of it, so whatever would be the output of

echo "$emails" | ./jq '.total_rows'

will so be stored in total_rows.

As mentioned in the comments by Tom Fenech, it is better to use $() for command substitution. It provides a better readability. So what you can do is:

total_rows=$(echo "$emails" | ./jq '.total_rows')
Nidhoegger
  • 4,973
  • 4
  • 36
  • 81