Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use "$@" (quoted dollar at) to pass parameters to a subscript. Like so ....</p> <p><strong><code>ls-color.sh:</code></strong></p> <pre><code>#!/bin/bash /bin/ls --color=auto "$@" # passes though all CLI-args to 'ls' </code></pre> <p><br></p> <h3>As to why.....</h3> <p>From the <a href="http://linux.die.net/man/1/bash" rel="noreferrer">Bash man page</a>:</p> <blockquote> <p><code>$*</code> -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, <code>"$*"</code> is equivalent to <code>"$1c$2c..."</code>, where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.</p> <p><code>$@</code> -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, <code>"$@"</code> is equivalent to <code>"$1" "$2" ...</code> If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, <code>"$@"</code> and <code>$@</code> expand to nothing (i.e., they are removed).</p> </blockquote> <hr> <p><em>Setting up some demo scripts ...</em></p> <pre><code>echo 'echo -e "\$1=$1\n\$2=$2\n\$3=$3\n\$4=$4"' &gt; echo-params.sh echo './echo-params.sh $*' &gt; dollar-star.sh echo './echo-params.sh $@' &gt; dollar-at.sh echo './echo-params.sh "$*"' &gt; quoted-dollar-star.sh echo './echo-params.sh "$@"' &gt; quoted-dollar-at.sh chmod +x *.sh </code></pre> <p><code>"$@"</code> - quoted-dollar-at is an <strong><em>identity transformation</em></strong> for re-passing args to a subshell (~99% of the time, this is what you meant to do):</p> <pre><code>./quoted-dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"' # $1= aaa # $2= # $3= 'cc cc' # $4= "ddd ddd" </code></pre> <p><code>"$*"</code> - quoted-dollar-star <strong><em>smashes the args into a single string</em></strong> (~1% of the time you actually want this behavior, eg in a conditional: <code>if [[ -z "$*" ]]; then ...</code>):</p> <pre><code>./quoted-dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"' # $1= aaa 'cc cc' "ddd ddd" # $2= # $3= # $4= </code></pre> <p><code>$*</code> / <code>$@</code> - without quotes, both forms strip off one level of quotation and interpret spaces from the underlying strings but ignore quotation characters (almost always, this is a mistake):</p> <pre><code>./dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"' # $1= aaa # $2= 'cc # $3= cc' # $4= "ddd ./dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"' # $1= aaa # $2= 'cc # $3= cc' # $4= "ddd </code></pre> <hr> <p>If you want to have some fun, you can use "$@" to nest things as deep as you'd like, pushing and popping elements off the args stack if you like.</p> <pre><code>function identity() { "$@" } set -x identity identity identity identity identity echo Hello \"World\" # + identity identity identity identity identity echo Hello '"World"' # + identity identity identity identity echo Hello '"World"' # + identity identity identity echo Hello '"World"' # + identity identity echo Hello '"World"' # + identity echo Hello '"World"' # + echo Hello '"World"' # Hello "World" </code></pre>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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