LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   difference between $* and $@ ? (https://www.linuxquestions.org/questions/linux-newbie-8/difference-between-%24%2A-and-%24%40-4175528869/)

szejiekoh 12-20-2014 01:47 PM

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

astrogeek 12-20-2014 02:10 PM

Quote:

Originally Posted by szejiekoh (Post 5288173)
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.

szejiekoh 12-20-2014 02:33 PM

Quote:

Originally Posted by astrogeek (Post 5288179)
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

astrogeek 12-20-2014 02:52 PM

Quote:

Originally Posted by szejiekoh (Post 5288186)
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!

rknichols 12-20-2014 06:48 PM

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.

rtmistler 12-22-2014 08:16 AM

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.


All times are GMT -5. The time now is 10:40 AM.