Using curl in a bash script and getting curl: (3) Illegal characters found in URL
<p>So I have a very simple bash script that is curl'ing to an auth server for a header. The header url is written to a var and then used in the next curl call. When using the var set in the first curl call I am getting "curl: (3) Illegal characters found in URL". I am able to echo the var and all looks good, I am even able to reset the var (in my example below) and it works.</p>

<p><strong>The Bash script</strong></p>

<pre><code>URL=$(curl -i -X GET -H "X-Auth-User: MyUserna,e" -H "X-Auth-Key: MyAPIKey" "https://urlToAuthServer.tld/auth/v1.0/" | grep "X-Storage-Url:" | awk '{print $2}')

curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt
</code></pre>

<p><strong>When running the above example I get:</strong></p>

<pre><code>curl: (3) Illegal characters found in URL
</code></pre>

<p><strong>The URL var looks like this (no illegal chars)</strong></p>

<pre><code>https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534
</code></pre>

<p><strong>When I do this in terminal it works:</strong></p>

<blockquote>
  <p>$ URL=$(curl -i -X GET -H "X-Auth-User: MyUserna,e" -H "X-Auth-Key: MyAPIKey" "<a href="https://urlToAuthServer.tld/auth/v1.0/" rel="noreferrer">https://urlToAuthServer.tld/auth/v1.0/</a>" | grep "X-Storage-Url:" | awk '{print $2}')</p>
  
  <p>$ echo $URL</p>
  
  <p><a href="https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534" rel="noreferrer">https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534</a></p>
</blockquote>

<p>Now I copy and paste the string and reasign it to URL like so (again all in terminal):</p>

<pre><code>&gt;$ URL="https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534"
&gt;$ curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt
</code></pre>

<p>It works.</p>

<p>So why am I getting the "curl: (3) Illegal characters found in URL" error in the first example?</p>

<p><strong>Update</strong>
I ran this:
<code>printf %s "$URL" | xxd</code></p>

<p>Here is the output (addressed changed up you get the idea)</p>

<pre><code>0000000: 6874 7470 733a 2f2f 6461 6c30 352e 6f62  https://server.ob
0000010: 6a65 6374 7374 6f72 6167 652e 736f 6674  jectstorage.lite
0000020: 6c61 7965 722e 6e65 742f 7631 2f41 5554  sabers.com/v1/AUT
0000030: 485f 6665 3235 3339 3434 2d38 6433 322d  H_aE2563981-7d32-
0000040: 3432 3138 2d61 6566 632d 6665 6638 3465  4201-bdoi-fef94a
0000050: 6166 3331 6232 0d                        ag11c8.
</code></pre>