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 output not correctly assinging to variable

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"

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

>Solution :

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.

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