-2

Below jq query output comes correctly.

ROUTE_ID= jq -r '.[][]? | select(.pattern? == "*test.com/testcards/email/*").id' route.json

route.json file contains a json output.

But echo "this is route $ROUTE_ID" or echo "this is route $ROUTE_ID does not return value for $ROUTE_ID"

oguz ismail
  • 39,105
  • 12
  • 41
  • 62

1 Answers1

1

What you are currently doing is setting the environment variable ROUTE_ID to nothing for the execution of jq, eg:

MY_ENV=abc command

Will set the environment variable "MY_ENV" to "abc" for the execution of command.

What you want to do is store the output of your command a variable, for this you'll need to use command substitutions:

my_var=$(command)

In your case:

route_id=$(jq -r '.[][]? | select(.pattern? == "test.com/testcards/email/").id' route.json)

Nitpicking; use lowercase variable names when possible, as UPPER_CASE are "reserved" for exported environment variables.

ikegami
  • 343,984
  • 15
  • 249
  • 495
Andreas Louv
  • 44,338
  • 13
  • 91
  • 116