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

2019-03-15 07:50发布

This question already has an answer here:

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?

标签: linux bash shell
3条回答
神经病院院长
2楼-- · 2019-03-15 08:11

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"

查看更多
Lonely孤独者°
3楼-- · 2019-03-15 08:19

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"
查看更多
狗以群分
4楼-- · 2019-03-15 08:20

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.

查看更多
登录 后发表回答