From: kaushal on
Hi,

http://tldp.org/LDP/abs/html/internalvariables.html#APPREF

I did not understand the difference between $@ and $* special
variable.
Please explain me with an example.

$*

All of the positional parameters, seen as a single word
$@

Same as $*, but each parameter is a quoted string, that is, the
parameters are passed on intact, without interpretation or expansion.
This means, among other things, that each parameter in the argument
list is seen as a separate word.

still not fully understood. Please suggest with an example.

Thanks,

Kaushal
From: David W. Hodgins on
On Sat, 05 Dec 2009 03:53:28 -0500, kaushal <kaushalshriyan(a)gmail.com> wrote:

> I did not understand the difference between $@ and $* special
> Please explain me with an example.

#!/bin/bash
# saved as ~/bin/parmdiff
showparms () {
count=0
for parm in "$*"; do
(( count++ ))
echo "count=$count, parm=$parm"
done
count=0
for parm in "$@"; do
(( count++ ))
echo "count=$count, parm=$parm"
done
}
showparms firstparm "secondparm thirdparm"

$ parmdiff
count=1, parm=firstparm secondparm thirdparm
count=1, parm=firstparm
count=2, parm=secondparm thirdparm

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
From: Jon LaBadie on
kaushal wrote:
> Hi,
>
> http://tldp.org/LDP/abs/html/internalvariables.html#APPREF
>
> I did not understand the difference between $@ and $* special
> variable.
> Please explain me with an example.
>
> $*
>
> All of the positional parameters, seen as a single word
> $@
>
> Same as $*, but each parameter is a quoted string, that is, the
> parameters are passed on intact, without interpretation or expansion.
> This means, among other things, that each parameter in the argument
> list is seen as a separate word.
>
> still not fully understood. Please suggest with an example.

Suppose you run "your_script" as follows:

$ your_script one 'two three' 'four five'

where argument 2 has one space and arg3 has 4,
and suppose your_script contains:

cmd $* # 1)
cmd $@ # 2)
cmd "$*" # 3)
cmd "$@" # 4)


Then 1) receives 5 arguments, no spaces,
i.e. each word from the three args is an arg to cmd
2) receives 5 arguments, no spaces,
ditto
3) receives 1 argument and six spaces,
i.e. one big argument
4) receives 3 arguments and five spaces,
i.e. the original args
From: Seebs on
set -- a b
for i in "$*"
do
echo $i
done

=> "a b"

for i in "$@"
do
echo $i
done

=> "a
b"

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Chris F.A. Johnson on
On 2009-12-05, kaushal wrote:
> Hi,
>
> http://tldp.org/LDP/abs/html/internalvariables.html#APPREF
>
> I did not understand the difference between $@ and $* special
> variable.
> Please explain me with an example.
>
> $*
>
> All of the positional parameters, seen as a single word
> $@
>
> Same as $*, but each parameter is a quoted string, that is, the
> parameters are passed on intact, without interpretation or expansion.
> This means, among other things, that each parameter in the argument
> list is seen as a separate word.
>
> still not fully understood. Please suggest with an example.

$* and $@ are identical. They expand to each word on the command line.

"$*" and "$@" differ. The former is a single argument containing
all the arguments on the command line. The latter expands to each
argument on the command line.

As an example, the sa function prints all arguments on the command
line in 4 different ways: using $*, $@, "$*" and "$@". Each
argument is printed on a separate line.

$ sa()
{
printf "%s\n" 'Using $*:' $* \
'Using $@:' $@ \
'Using "$*":' "$*" \
'Using "$@":' "$@"
}
$ sa 1 '2 3' 4 ## 3 arguments containing 4 words
Using $*:
1
2
3
4
Using $@:
1
2
3
4
Using "$*":
1 2 3 4
Using "$@":
1
2 3
4


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====