title: Bash: "printf %q $str" deletes spaces when in scripts. (Alternatives?)
body: <p><strong>printf %q</strong> should quote a string.  However, when executed into a script, it deletes the spaces.</p>
  
  <p>This command:</p>
  
  <pre><code>printf %q "hello world"
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>hello\ world
  </code></pre>
  
  <p>which is correct.</p>
  
  <p>This script:</p>
  
  <pre><code>#!/bin/bash
  
  str="hello world"
  printf %q $str
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>helloworld
  </code></pre>
  
  <p>which is wrong.</p>
  
  <p>If such behavior is indeed expected, what alternative exists in a script for quoting a string containing any character in a way that it can be translated back to the original by a called program?</p>
  
  <p>Thanks.</p>
  
  <p>Software: GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)</p>
  
  <p>EDITED: Solved, thanks.</p>