Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other answers cover your real problem. But one thing worth saying more about is how <code>$*</code> and <code>$@</code> differ in expansion when double quoted. <strong>tl;dr</strong> You almost always want <code>"$@"</code>.</p> <p><code>"$*"</code> expands to <strong>a single string</strong> containing all the parameters with the first character of <code>IFS</code> in between them. In practice this usually means all params as one string separated by spaces. So if your params are <code>file1.txt</code>, <code>file2.txt</code> and <code>file3.txt</code>, then <code>"$*"</code> hands that to the loop as <strong>one thing</strong>, namely <code>file1.txt file2.txt file3.txt</code>. That one thing, of course, does not exist as such.</p> <p><code>"$@"</code> expands to <strong>each of the parameters</strong>, one at a time, treated as a single "word". This is good because it will treat <code>file1.txt</code>, <code>file2.txt</code> and <code>file3.txt</code> from the earlier example correctly as <strong>three items</strong>. The loop will receive them correctly one at a time. In addition, <code>"$@"</code> is good since even if your filenames have spaces in them, you will get the right behavior since the loop will handle, e.g. <code>filename 1</code>, <code>filename 2</code>, <code>filename 2 and a half</code> as three items, one by one. (Normally the shell treats a space as marking the end of a word, so <code>filename 1</code> would look like <strong>two items</strong> to the shell, <code>filename</code> and <code>1</code>.</p> <p>When they aren't quoted both <code>$@</code> and <code>$*</code> expand the words of all the positional parameters. So in that case, they are effectively identical (and both a bad idea since they don't protect you from oddly named files).</p> <p>There's a good visualization of all possibilities here: <a href="http://wiki.bash-hackers.org/scripting/posparams#mass_usage" rel="nofollow">http://wiki.bash-hackers.org/scripting/posparams#mass_usage</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload