<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Software is Crap]]></provider_name><provider_url><![CDATA[https://davmac.wordpress.com]]></provider_url><author_name><![CDATA[davmac]]></author_name><author_url><![CDATA[https://davmac.wordpress.com/author/davmac/]]></author_url><title><![CDATA[Using $* in shell scripts is Nearly Always&nbsp;Wrong]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>(Bourne and Bash) Shell script writers bandy &#8216;$*&#8217; about like it&#8217;s a stick of celery, but the truth is $* is nearly always the wrong thing to use. (For the clueless, $* expands to all the &#8220;positional parameters&#8221;, that is, all the parameters to the current script or function; it&#8217;s useful in cases where you want to pass the argument values for those parameters on to another program).</p>
<p>So why is $* wrong? <em>Because it is evaluated before word splitting</em>. In other words, if any of the arguments to your script had a space or other whitespace, then those arguments will become two when $* is evaluated. The following shell script can be used to verify this:</p>
<blockquote><p>#!/bin/sh<br />
# Call this script &#8220;mytouch&#8221;<br />
touch $*</p></blockquote>
<p>&#8230; Now try to &#8220;mytouch&#8221; a file with a space in its name (yeah, well done, you just created two files with one command).</p>
<p>Does quoting it work? will &#8220;$*&#8221; solve your problems? It will fix the above example, right up to the point where you try to &#8220;mytouch&#8221; two or more files at once and then you&#8217;ll see the fallacy (hint: &#8220;$*&#8221; always expands to a single &#8220;word&#8221;).</p>
<p>So what is the correct solution? It&#8217;s simple: use &#8220;$@&#8221; instead. Note, that&#8217;s $@, with quotes around it. Hence &#8220;$@&#8221;. This specifically expands to one string for each actual argument. It works. Hallelujah. (And while I&#8217;m blathering on shell scripts, note that any unquoted variable expansion is also Nearly Always Wrong for pretty much the same reasons).</p>
]]></html></oembed>