Skip to main content
moved explanation of each case to the top of the code block to make the reading flow more straightforward
Source Link

They aren't the same. $* is a single string, whereas $@ is an actual array. To see the difference, execute the following script like so:

 > ./test.sh one two "three four" 

The script:

#!/bin/bash echo "Using \"\$*\":" for a in "$*"; do echo $a; done echo -e "\nUsing \$*:" for a in $*; do echo $a; done echo -e "\nUsing \"\$@\":" for a in "$@"; do echo $a; done echo -e "\nUsing \$@:" for a in $@; do echo $a; done 

The explanation and the results for the four cases are: below.

Using "$*": one two three four 

HereIn the first case, the parameters are regarded as one long quoted string. Unquoted:

Using $*"$*": one   two   three   four 

TheCase 2 (unquoted) - the string is broken into words by the for loop.:

Using "$@"$*: one two three   four 

ThisCase 3 - it treats each element of $@ as a quoted string.:

Using $@"$@": one two three   four 

ThisLast case - it treats each element as an unquoted string, so the last one is again split by what amounts to for three four.:

Using $@: one two three four 

They aren't the same. $* is a single string, whereas $@ is an actual array. To see the difference, execute the following script like so:

 > ./test.sh one two "three four" 

The script:

#!/bin/bash echo "Using \"\$*\":" for a in "$*"; do echo $a; done echo -e "\nUsing \$*:" for a in $*; do echo $a; done echo -e "\nUsing \"\$@\":" for a in "$@"; do echo $a; done echo -e "\nUsing \$@:" for a in $@; do echo $a; done 

The four cases are:

Using "$*": one two three four 

Here, the parameters are regarded as one long quoted string. Unquoted:

Using $*: one  two  three  four 

The string is broken into words by the for loop.

Using "$@": one two three four 

This treats each element of $@ as a quoted string.

Using $@: one two three  four 

This treats each element as an unquoted string, so the last one is again split by what amounts to for three four.

They aren't the same. $* is a single string, whereas $@ is an actual array. To see the difference, execute the following script like so:

 > ./test.sh one two "three four" 

The script:

#!/bin/bash echo "Using \"\$*\":" for a in "$*"; do echo $a; done echo -e "\nUsing \$*:" for a in $*; do echo $a; done echo -e "\nUsing \"\$@\":" for a in "$@"; do echo $a; done echo -e "\nUsing \$@:" for a in $@; do echo $a; done 

The explanation and the results for the four cases are below.

In the first case, the parameters are regarded as one long quoted string:

Using "$*": one two three four 

Case 2 (unquoted) - the string is broken into words by the for loop:

Using $*: one two three  four 

Case 3 - it treats each element of $@ as a quoted string:

Using "$@": one two three four 

Last case - it treats each element as an unquoted string, so the last one is again split by what amounts to for three four:

Using $@: one two three four 
Source Link
goldilocks
  • 90.1k
  • 33
  • 212
  • 274

They aren't the same. $* is a single string, whereas $@ is an actual array. To see the difference, execute the following script like so:

 > ./test.sh one two "three four" 

The script:

#!/bin/bash echo "Using \"\$*\":" for a in "$*"; do echo $a; done echo -e "\nUsing \$*:" for a in $*; do echo $a; done echo -e "\nUsing \"\$@\":" for a in "$@"; do echo $a; done echo -e "\nUsing \$@:" for a in $@; do echo $a; done 

The four cases are:

Using "$*": one two three four 

Here, the parameters are regarded as one long quoted string. Unquoted:

Using $*: one two three four 

The string is broken into words by the for loop.

Using "$@": one two three four 

This treats each element of $@ as a quoted string.

Using $@: one two three four 

This treats each element as an unquoted string, so the last one is again split by what amounts to for three four.