title: Parsing a curl json response and using response to build another request
body: <p>I am trying to build an shell script that will fetch the image url for a random XKCD comic, so that I can display it using Übersicht. Since you can only ask the XKCD API for the latest comic or a specific comic I need to:</p>
  
  <ol>
  <li>Send a <code>GET</code> request to <code>http://xkcd.com/info.0.json</code>, fetch the value of the <code>num</code> element.</li>
  <li>Send another request to <code>http://xkcd.com/XXX/info.0.json</code> where <code>XXX</code>is the value of  <code>num</code>.</li>
  </ol>
  
  <p>My current command looks like this and successfully returns the comic number:</p>
  
  <pre><code>curl -s 'http://xkcd.com/1510/info.0.json' | grep -Eo '"num": \d+' | grep -Eo '\d+'
  </code></pre>
  
  <ol>
  <li>I haven't been able to figure out how to use capture groups with <code>grep</code>, so I need to grep the json twice. The common advice is to use <code>-P</code>, which is not supported in Mac OS X 10.10.</li>
  <li>I have no idea how to read the output of grep (as <code>XXX</code>) into the second <code>curl -s 'http://xkcd.com/XXX/info.0.json'</code> command.</li>
  </ol>