Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

up vote 11 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.