Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Which are the differences between $@ and $* ? For example I see no differences here:

#! /bin/bash
echo with star: $*
echo with at sign: $@
share|improve this question
up vote 12 down vote accepted

When they're not quoted, there is no difference between $@ and $*. Both are equal to $1 $2 … But don't do this.

With double quotes, "$@" expands each element as an argument:

"$1" "$2"

while "$*" expands to all elements merged into one argument:

"$1c$2c..."

where c is the first character of IFS. If you unset IFS, c will be a space.

You almost always want "$@". The same goes for arrays: "${myarray[@]}"

share|improve this answer
1  
I only disagree with the last statement. I use "$*" expansion with IFS all the time. It's as useful as array.join in other languages. You can even use it to break lines: Whereas printf '%s\n' "$@" will have a trailing linebreak, printf '%s' "$*" with IFS='\n' will not. – kojiro Jan 20 '13 at 21:11
    
(Sorry, that should be IFS=$'\n'.) – kojiro Jan 20 '13 at 21:33

@sputnick has the right answer. To demonstrate with an example:\

# use two positional parameters, each with whitespace
set -- "one two" "three four"

# demonstrate the various forms with the default $IFS
echo '$*:'
printf ">%s<\n" $*
echo '$@:'
printf ">%s<\n" $@
echo '"$*":'
printf ">%s<\n" "$*"
echo '"$@":'
printf ">%s<\n" "$@"

# now with a custom $IFS
(
IFS=:
echo '$*:'
printf ">%s<\n" $*
echo '$@:'
printf ">%s<\n" $@
echo '"$*":'
printf ">%s<\n" "$*"
echo '"$@":'
printf ">%s<\n" "$@"
)

outputs

$*:
>one<
>two<
>three<
>four<
$@:
>one<
>two<
>three<
>four<
"$*":
>one two three four<
"$@":
>one two<
>three four<
$*:
>one two<
>three four<
$@:
>one two<
>three four<
"$*":
>one two:three four<
"$@":
>one two<
>three four<
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .