LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   The differece between $@ and $* (https://www.linuxquestions.org/questions/linux-newbie-8/the-differece-between-%24%40-and-%24%2A-416836/)

johnny_jiang 02-18-2006 09:44 AM

The differece between $@ and $*
 
Hello,everyone

I'm a linux newbie from Shanghai.There's a question regarding $@ and $* which has confused me for a long time.Actually I have read the bash manual and some bibliographies,I found that they just talk about these two positional parameters' difference when they're double quoted.So what if they're assigned to a variable?
I've done some tests below.

Code:

set -- "First Arg" "Second" "Third:Arg" "" "Fifth: :Arg"

IFS=:

var=$@
var1=$*

echo "$var"  # First Arg Second Third Arg  Fifth  Arg
echo "$var1" # First Arg:Second:Third:Arg::Fifth: :Arg

IFS=

var=$@
var1=$*

echo "$var"  # First Arg Second Third:Arg  Fifth: :Arg
echo "$var1" # First Arg Second Third:Arg  FFiifftthh::  ::AArrg

It's too weird for me to understand.

unSpawn 02-18-2006 11:42 AM

I'm a linux newbie from Shanghai.
Doesn't mean nothing, Im a noob from Hell when it comes to open heart surgery :-]


AFAIK \* is the concatenation (whole string) while @ the IFS-sep'ped parts:

]$ tellMeIWannaKnow() { i=("$*"); echo ${#i[@]}; }
]$ tellMeIWannaKnow onetwothree four
1

]$ tellMeIWannaKnow() { i=("$@"); echo ${#i[@]}; }
]$ tellMeIWannaKnow onetwothree four
2

johnny_jiang 02-18-2006 10:51 PM

Thank you,unSpawn!

I understand what you're talking about."$@" will seperate arguments with IFS.But there's one thing I am also a little fuzzy on it.

Code:

var=$@

echo "$var"  # First Arg Second Third Arg  Fifth  Arg

How to explain how that runs?Why do the colons disappear?


All times are GMT -5. The time now is 03:00 PM.