title: Escaping characters in bash (for JSON)
body: <p>I'm using git, then posting the commit message and other bits as a JSON payload to a server.</p>
  
  <p>Currently I have:</p>
  
  <pre><code>MSG=`git log -n 1 --format=oneline | grep -o ' .\+'`
  </code></pre>
  
  <p>which sets MSG to something like:</p>
  
  <pre><code>Calendar can't go back past today
  </code></pre>
  
  <p>then</p>
  
  <pre><code>curl -i -X POST \
    -H 'Accept: application/text' \
    -H 'Content-type: application/json' \
    -d "{'payload': {'message': '$MSG'}}" \
    'https://example.com'
  </code></pre>
  
  <p>My real JSON has another couple of fields.</p>
  
  <p>This works fine, but of course when I have a commit message such as the one above with an apostrophe in it, the JSON is invalid.</p>
  
  <p>How can I escape the characters required in bash? I'm not familiar with the language, so am not sure where to start. Replacing <code>'</code> with <code>\'</code> would do the job at minimum I suspect.</p>