ZhangZhihui's Blog  

There are no difference between $* and $@:

zzh@ZZHPC:~/aaa$ cat 1.sh
mkdir $*
zzh@ZZHPC:~/aaa$ cat 2.sh
mkdir $@
zzh@ZZHPC:
~/aaa$ ./1.sh a "b c" d zzh@ZZHPC:~/aaa$ ls -1 1.sh 2.sh a b c d zzh@ZZHPC:~/aaa$ rmdir a b c d
zzh@ZZHPC:
~/aaa$ ./2.sh a "b c" d zzh@ZZHPC:~/aaa$ ls -1 1.sh 2.sh a b c d zzh@ZZHPC:~/aaa$ rmdir a b c d

 

 

but there is a difference between "$@" and "$*":

zzh@ZZHPC:~/aaa$ cat 3.sh
mkdir "$*"
zzh@ZZHPC:~/aaa$ cat 4.sh
mkdir "$@"
zzh@ZZHPC:~/aaa$ ./3.sh a "b c" d zzh@ZZHPC:~/aaa$ ls -1 3.sh 4.sh 'a b c d' zzh@ZZHPC:~/aaa$ rmdir 'a b c d'
zzh@ZZHPC:~/aaa$ ./4.sh a "b c" d zzh@ZZHPC:~/aaa$ ls -1 1.sh 2.sh 3.sh 4.sh a 'b c' d

 

We gave three arguments to the script but in "$*" they all were merged into one argument 'a b c d'.

You can see here, that "$*" means always one single argument, and "$@" contains as many arguments, as the script had. "$@" is a special token which means "wrap each individual argument in quotes". So a "b c" d becomes (or rather stays) "a" "b c" "d" instead of "a b c d" ("$*") or "a" "b" "c" "d" ($@ or $*).

Copied from: https://stackoverflow.com/questions/15596826/what-is-the-difference-between-and-in-shell-script

posted on 2019-10-14 15:02  ZhangZhihuiAAA  阅读(191)  评论(0编辑  收藏  举报