Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a string from a curl response for a variable in bash?

Tags:

grep

bash

curl

The bash script sends a curl. The curl response example is following:

{"code":"2aaea70fdccd7ad11e4ee8e82ec26162","nonce":1541355854942}

I need to get the code "2aaea70fdccd7ad11e4ee8e82ec26162" (without quotes) and use it in the bash script.

like image 491
M2bandit Avatar asked Sep 16 '25 16:09

M2bandit


1 Answers

Use jq to extract the value from the JSON, and command substitution to capture the output of the command:

code=$(curl ... | jq -r '.code')

The -r (--raw) prints the string directly instead of quoting it as in a JSON.

like image 86
choroba Avatar answered Sep 18 '25 10:09

choroba