<p>What's going on here?</p>

<p>printf.sh:</p>

<pre><code>#! /bin/sh
NAME="George W. Bush"
printf "Hello, %s\n" $NAME
</code></pre>

<p>Command line session:</p>

<pre><code>$ ./printf.sh
Hello, George
Hello, W.
Hello, Bush
</code></pre>

<p>UPDATE: <code>printf "Hello, %s\n" "$NAME"</code> works. For why I'm not using <code>echo</code>, consider</p>

<p>echo.sh:</p>

<pre><code>#! /bin/sh
FILE="C:\tmp"
echo "Filename: $FILE"
</code></pre>

<p>Command-line:</p>

<pre><code>$ ./echo.sh
Filename: C:    mp
</code></pre>

<p>The <a href="http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html" rel="noreferrer">POSIX spec for <code>echo</code></a> says, "New applications are encouraged to use <code>printf</code> instead of <code>echo</code>" (for this and other reasons). </p>

[{'answer_id': 42087, 'author': 'Redbaron', 'author_id': 41, 'author_profile': 'https://Stackoverflow.com/users/41', 'pm_score': 0, 'selected': False, 'text': '<p>If you want all of those words to be printed out on their own, use <strong>print</strong> instead of <strong>printf</strong> </p>\n\n<p><strong>printf</strong> takes the formatting specification and applies it to each argument that you pass in. Since you have three arguments {George, W., Bush}, it outputs the string three times using the different arguments.</p>\n'}, {'answer_id': 42089, 'author': 'Tanj', 'author_id': 4275, 'author_profile': 'https://Stackoverflow.com/users/4275', 'pm_score': 1, 'selected': False, 'text': '<p>is there a specific reason you are using printf or would echo work for you as well?</p>\n\n<pre><code>NAME="George W. Bush"\necho "Hello, "$NAME\n</code></pre>\n\n<p>results in </p>\n\n<pre><code>Hello, George W. Bush\n</code></pre>\n\n<p>edit:\nThe reason it is iterating over "George W. Bush" is because the bourne shell is space delimitted. To keep using printf you have to put $NAME in double quotes</p>\n\n<pre><code>printf "Hello, %s\\n" "$NAME"\n</code></pre>\n'}, {'answer_id': 42092, 'author': 'ColinYounger', 'author_id': 1223, 'author_profile': 'https://Stackoverflow.com/users/1223', 'pm_score': 4, 'selected': True, 'text': '<p>Your NAME variable is being substituted like this:</p>\n\n<pre><code>printf "Hello, %s\\n" George W. Bush\n</code></pre>\n\n<p>Use this:</p>\n\n<pre><code>#! /bin/sh\nNAME="George W. Bush"\nprintf "Hello, %s\\n" "$NAME"\n</code></pre>\n'}, {'answer_id': 42103, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 1, 'selected': False, 'text': '<p>The way I interpret the <a href="http://www.ss64.com/bash/printf.html" rel="nofollow noreferrer">man page</a> is it considers the string you pass it to be an argument; if your string has spaces it thinks you are passing multiple arguments.  I believe ColinYounger is correct by surrounding the variable with quotes, which forces the shell to interpret the string as a single argument.</p>\n\n<p>An alternative might be to let printf expand the variable:</p>\n\n<pre><code>printf "Hello, $NAME."\n</code></pre>\n\n<p>The links are for bash, but I am pretty sure the same holds for sh.</p>\n'}]
['https://Stackoverflow.com/questions/42070', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1412/']