LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash $@, $*, "$*", "$@" (http://www.linuxquestions.org/questions/linux-general-1/bash-%24%40-%24%2A-%24%2A-%24%40-873997/)

mzh 04-09-2011 10:50 AM

Bash $@, $*, "$*", "$@"
 
Dear People,
could someone please give a brief description of what the title mentioned expressions do? I googled it but all i find is weired useless quote pages (the internet is getting worse and worse it seems).
Especially I would be interested in understanding what the difference between the unquoted and quoted versions of the same expression is, ie. $@ vs "$@".

Thanks for any hints or posts.

H_TeXMeX_H 04-09-2011 11:41 AM

The answer is here:
http://www.grymoire.com/Unix/Sh.html#uh-42

Telengard 04-09-2011 11:54 AM

The difference is word splitting. The most common case where this matters is if you have file names with spaces in them. Here is my unsophisticated explanation.
  • $* arguments are separated by spaces
  • "$*" means space does not separate arguments
  • $@ arguments are separated by spaces
  • "$@" arguments are separated by unquoted spaces

This is all fine and good until you bring IFS into the mix. I'll leave it to others to go into that except to say that you shouldn't change IFS from its default value unless you have a very specific need to do so and you remember to change it back immediately.

So here's a short, simple Bash script to demonstrate what's happening.

Code:

#! /bin/bash

echo 'parsing with $*'
i=0
for a in $*
do
  i=$((i+1))
  echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with "$*"'
i=0
for a in "$*"
do
  i=$((i+1))
  echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with $@'
i=0
for a in $@
do
  i=$((i+1))
  echo "arg $i = \`$a'"
done
echo "$i args passed"

echo

echo 'parsing with "$@"'
i=0
for a in "$@"
do
  i=$((i+1))
  echo "arg $i = \`$a'"
done
echo "$i args passed"

Here's a sample run.

Code:

foo$ ./parseargs one two
parsing with $*
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$*"
arg 1 = `one two'
1 args passed

parsing with $@
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$@"
arg 1 = `one'
arg 2 = `two'
2 args passed
foo$

What happens when we quote the space between arguments?

Code:

foo$ ./parseargs one\ two
parsing with $*
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$*"
arg 1 = `one two'
1 args passed

parsing with $@
arg 1 = `one'
arg 2 = `two'
2 args passed

parsing with "$@"
arg 1 = `one two'
1 args passed
foo$

What happens when we pass no arguments at all?

Code:

foo$ ./parseargs
parsing with $*
0 args passed

parsing with "$*"
arg 1 = `'
1 args passed

parsing with $@
0 args passed

parsing with "$@"
0 args passed
foo$

Most times you will probably want "$@", but there may be cases where the others are more desirable.

I invite informative expansions and corrections on all the above.

HTH

mzh 04-09-2011 12:44 PM

@Telengard: Thank you a lot for the comprehensive explanation. This makes it a lot easier for me to understand it.

Telengard 04-09-2011 03:16 PM

If you feel your question has been adequately answered then please consider using th thread tools to mark this thread solved.

Your blog is nifty. I like that you use it to share the solutions you find.
:)

mzh 04-10-2011 03:20 PM

Quote:

Originally Posted by Telengard (Post 4319409)
If you feel your question has been adequately answered then please consider using th thread tools to mark this thread solved.

Your blog is nifty. I like that you use it to share the solutions you find.
:)

Even more thanks for that. Yeah, its kind of a convenient way to keep track of solutions of particular use to the kind of things i need to be able to do.


All times are GMT -5. The time now is 02:17 PM.