2013-03-24 89 views
5

在我script.sh

[email protected] 
bb=$* 
echo $aa 
echo $bb 

运行它时:

source script.sh a b c d e f g 

我得到:

a b c d e f g 
a b c d e f g 

是什么[email protected]$*之间的区别?

+0

@mat为什么谷歌没有找到所有其他的重复? – 0x90 2013-03-24 09:38:40

+1

@ 0x90:Google不索引“标点符号”,只是单词;尝试[symbolhound](http://symbolhound.com/?q=%24%40+%24%2A)这样的事情。 – 2013-03-24 12:27:29

回答

8

$*[email protected]之间没有区别,但是"[email protected]""$*"之间存在差异。

$ cat 1.sh 
mkdir "$*" 

$ cat 2.sh 
mkdir "[email protected]" 

$ sh 1.sh a "b c" d 

$ ls -l 
total 12 
-rw-r--r-- 1 igor igor 11 mar 24 10:20 1.sh 
-rw-r--r-- 1 igor igor 11 mar 24 10:20 2.sh 
drwxr-xr-x 2 igor igor 4096 mar 24 10:21 a b c d 

我们给了三个参数的脚本(ab cd),但在“$ *”他们都被合并为一个参数a b c d

$ sh 2.sh a "b c" d 

$ ls -l 
total 24 
-rw-r--r-- 1 igor igor 11 mar 24 10:20 1.sh 
-rw-r--r-- 1 igor igor 11 mar 24 10:20 2.sh 
drwxr-xr-x 2 igor igor 4096 mar 24 10:21 a 
drwxr-xr-x 2 igor igor 4096 mar 24 10:21 a b c d 
drwxr-xr-x 2 igor igor 4096 mar 24 10:21 b c 
drwxr-xr-x 2 igor igor 4096 mar 24 10:21 d 

您可以在这里看到,这"$*"意味着总有一个单独的参数,并"[email protected]"包含许多参数,如脚本了。 “$ @”是一个特殊的标记,意思是“用引号包裹每个单独的参数”。因此a "b c" d变成(或者说停留)"a" "b c" "d"而不是"a b c d""$*")或"a" "b" "c" "d"[email protected]$*)。

另外,我建议的主题是这个美丽阅读:

http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST

相关问题