Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.