difference between $@ and $* in bash script [dupli

2019-03-15 07:24发布

问题:

This question already has an answer here:

  • Accessing bash command line args $@ vs $* 5 answers

There are 4 bash snippets below. I call them with ./script.sh a b c

for arg in $@; do 
echo "$arg"
done   ## output "a\nb\nc"

for arg in "$@"; do
echo "$arg"
done  ## output "a\nb\nc" -- I don't know why

for arg in $*; do
echo "$arg"
done  ##    output "a\nb\nc"

for arg in "$*"; do
echo "$arg"
done    ## output "abc"

I don't know what is the exact difference between $@ and $*,
and I think "$@" and "$*" should be the same, but they are not. Why?

回答1:

If you have a script foo.sh:

asterisk "$*"
at-sign "$@"

and call it with:

./foo.sh "a a" "b b" "c c"

it's equivalent to:

asterisk "a a b b c c"
at-sign "a a" "b b" "c c"

Without the quotes, they're the same:

asterisk $*
at-sign $@

would be equivalent to:

asterisk "a" "a" "b" "b" "c" "c"
at-sign "a" "a" "b" "b" "c" "c"


回答2:

Difference between $* and $@ is::

"$*" All the positional parameters (as a single word) *

"$@" All the positional parameters (as separate strings)

If you pass three command-line arguments given to a bash script to a C program using ./my_c $@,

you get the result ARG[1] == "par1" ARG[2] == "par2" ARG[3] == "par3"

If you pass three command-line arguments given to a bash script to a C program using ./my_c $*,

you get the result ARG[1] == "par1 par2 par3"



回答3:

This matters in shell scripts: for example the script testargs.sh

#! /bin/bash -p

echo $#

for i in $(seq 1 $#)
do
    echo "$i: ${!i}"
done

for val in "$@"; do
    echo "in quote @, $val"
done

for val in "$*"; do
    echo "in quote *, $val"
done

for val in $@; do
    echo "not in quote @, $val"
done

for val in $*; do
    echo "not in quote *, $val"
done

If this script is executed as /tmp/testargs.sh a b c 'd e', the results are:

4
1: a
2: b
3: c
4: d e
in quote @, a
in quote @, b
in quote @, c
in quote @, d e
in quote *, a b c d e
not in quote @, a
not in quote @, b
not in quote @, c
not in quote @, d
not in quote @, e
not in quote *, a
not in quote *, b
not in quote *, c
not in quote *, d
not in quote *, e

Thus, if number of arguments are to be preserved, always use "$@" or iterate through each argument using the for i in $(seq 1 $#) loop. Without quotes, both are same.



标签: linux bash shell