LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-20-2014, 02:47 PM   #1
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
difference between $* and $@ ?


Hi all,

I done a simple script and find no difference between $* and $@.

Code:
#!/bin/bash
echo "The parameters given are $@"
echo "The parameters given are $*"

Sun Dec 21 03:45:22 SGT 2014 > bash test5.sh 1 2 3 4 5
The parameters given are 1 2 3 4 5
The parameters given are 1 2 3 4 5
but have seen recommendations to use $@ instead. Why ?

Regards,
Noob
 
Old 12-20-2014, 03:10 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,885
Blog Entries: 1

Rep: Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005
Quote:
Originally Posted by szejiekoh View Post
Hi all,

I done a simple script and find no difference between $* and $@.
...
but have seen recommendations to use $@ instead. Why ?
Which you should use would depend on what you were trying to do with them and in what context.

For the actual differences see man bash Special Parameters section, then decide which suits your own use case.

As for those recommending to use one over the other, you should ask them why so, or consider their context.
 
Old 12-20-2014, 03:33 PM   #3
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Which you should use would depend on what you were trying to do with them and in what context.

For the actual differences see man bash Special Parameters section, then decide which suits your own use case.

As for those recommending to use one over the other, you should ask them why so, or consider their context.
Hi Astrogeek,

Thanks for replying. Maybe i understood the context $* wrongly, but it doesn't showing what i intended it to show.

Code:
Sun Dec 21 04:33:19 SGT 2014 > cat test6.sh
#!/bin/bash
echo "Listing all Parameters Below"
echo "$*"

Sun Dec 21 04:32:11 SGT 2014 > export IFS='|'
Sun Dec 21 04:32:50 SGT 2014 > test6.sh 1 2 3 4 5 6
Listing all Parameters Below
1 2 3 4 5 6
Isn't it suppose to be showing "1|2|3|4|5|6" ?

Regards,
Noob
 
Old 12-20-2014, 03:52 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,885
Blog Entries: 1

Rep: Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005Reputation: 2005
Quote:
Originally Posted by szejiekoh View Post
Hi Astrogeek,

Thanks for replying. Maybe i understood the context $* wrongly, but it doesn't showing what i intended it to show.

Code:
Sun Dec 21 04:33:19 SGT 2014 > cat test6.sh
#!/bin/bash
echo "Listing all Parameters Below"
echo "$*"

Sun Dec 21 04:32:11 SGT 2014 > export IFS='|'
Sun Dec 21 04:32:50 SGT 2014 > test6.sh 1 2 3 4 5 6
Listing all Parameters Below
1 2 3 4 5 6
Isn't it suppose to be showing "1|2|3|4|5|6" ?
From the bash man page (scroll up a page or two above the IFS definition)...

Quote:
The following variables are used by the shell. In some cases, bash assigns a default value to a vari‐
able; these cases are noted below.
...
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines
into words with the read builtin command. The default value is ``<space><tab><newline>''.
That says that bash assigns a default, not that it assigns a default if one is not set...

So if you set IFS inside your shell that is what you will see, but if exported from outside the shell it will be overridden by the default.

Try it!
 
1 members found this post helpful.
Old 12-20-2014, 07:48 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,962

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
It going to be confusing to test by changing IFS because it affects both the way the argument string is interpreted and the way the output appears. Here is a script that demonstrates the difference:
Code:
$ cat atest.sh
#!/bin/bash
echo 'Args as seen by $*:'
N=0
for Arg in $*; do
    echo "Arg $((++N)) is '$Arg\`"
done
echo
echo 'Args as seen by $@:'
N=0
for Arg in $@; do
    echo "Arg $((++N)) is '$Arg\`"
done
echo
echo 'Args as seen by "$*":'
N=0
for Arg in "$*"; do
    echo "Arg $((++N)) is '$Arg\`"
done
echo
echo 'Args as seen by "$@":'
N=0
for Arg in "$@"; do
    echo "Arg $((++N)) is '$Arg\`"
done



$ bash atest.sh "first arg" "second arg" last1
Args as seen by $*:
Arg 1 is 'first`
Arg 2 is 'arg`
Arg 3 is 'second`
Arg 4 is 'arg`
Arg 5 is 'last1`

Args as seen by $@:
Arg 1 is 'first`
Arg 2 is 'arg`
Arg 3 is 'second`
Arg 4 is 'arg`
Arg 5 is 'last1`

Args as seen by "$*":
Arg 1 is 'first arg second arg last1`

Args as seen by "$@":
Arg 1 is 'first arg`
Arg 2 is 'second arg`
Arg 3 is 'last1`
The difference becomes important when you are passing arguments that have embedded white space. Using "$@" (with the quotes) is the only way to get both (a) the arguments handled separately, and (b) arguments with embedded white space kept as single args.
 
1 members found this post helpful.
Old 12-22-2014, 09:16 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,105
Blog Entries: 10

Rep: Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524Reputation: 1524
http://tldp.org/LDP/abs/html/interna...s.html#ARGLIST

Read this. Tells you all you need to know about the subject, cites specifically the differences between $* and $@ and shows an example in script form where you can copy/paste/run that to test for yourself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Making a Difference; Selling a Difference LXer Syndicated Linux News 0 09-23-2010 06:00 AM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 05:26 AM
difference chamanchi_hari Linux - Newbie 3 06-16-2009 09:12 AM
What is the difference between the two? YESnewbie2linux SUSE / openSUSE 4 02-12-2007 07:45 AM
What is the difference? pearsonx4 Linux - Newbie 4 10-05-2003 11:34 PM


All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration