title: Invalid string: control characters from U+0000 through U+001F must be escaped using Bash?
body: <p>I have a function like:</p>
  <pre><code>getServers() {
      curl -s -X GET ...
  }
  </code></pre>
  <p>The output is a complex JSON string. I can parse the JSON like this:</p>
  <pre><code>serverId=$(getServers | jq -r &quot;.[] | select(whatever...)&quot;)
  echo $serverId
  </code></pre>
  <p>But if I store the output of the function call in a variable, I get an error:</p>
  <pre><code>servers=$(getServers)
  echo $servers | jq .
  # jq can not process this
  # parse error: Invalid string: control characters from U+0000 through             
  echo &quot;$servers&quot; | jq .
  # does not work either
  </code></pre>
  <p>U+001F must be escaped at line ...</p>
  <p>Even though I <code>$servers</code> contain the same value as function call, <code>jq</code> fails to process it. What's happening here?</p>
  <blockquote>
  <p>$ jq --version<br />
  jq-1.5</p>
  </blockquote>
  <br>
  <p>This is not a duplicate question! This issue roots in existence of <code>\r\n</code> in the string. Adding quotes does not fix the problem.</p>
  <h2>Answer</h2>
  <p>Pipe the string into <code>tr '\r\n' ' '</code>:</p>
  <pre><code>servers=$(getServers)
  echo $servers | tr '\r\n' ' ' | jq .
  </code></pre>
  <p>This script and its output show how adding <code>&quot;</code> does not fix the problem, but <code>tr</code> does:</p>
  <pre><code>#!/bin/env bash
  
  get() {
      x=$'multi\r\nline'
      echo $x
  }
  
  
  g=$(get)
  
  echo 'Without &quot;'
  echo $g
  echo 'With &quot;'
  echo &quot;$g&quot;
  echo 'With tr'
  echo $g | tr '\r\n' '\n'
  </code></pre>
  <p>Output:</p>
  <pre><code>Without &quot;
   line
  With &quot;
   line
  With tr
  multi
   line
  </code></pre>
  <p>I hope community votes to reopen this question.</p>