2013-03-24 4 views
5

in:

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

, wenn es ausgeführt wird:

source script.sh a b c d e f g 

ich:

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

Was ist der Unterschied zwischen [email protected] und $*?

+0

@mat Warum Google hat alle anderen Duplikate nicht finden? – 0x90

+1

@ 0x90: Google indiziert nicht "Interpunktion", nur Wörter; versuchen Sie [Symbolhound] (http://symbolhound.com/?q=%24%40+%24%2A) für solche Dinge. –

Antwort

8

Es gibt keinen Unterschied zwischen $* und [email protected], aber es gibt einen Unterschied zwischen "[email protected]" und "$*".

$ 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 

Wir haben drei Argumente an das Skript (a, b c und d), aber in "$ *" sie alle zu einem Argument a b c d verschmolzen wurden.

$ 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 

Sie können hier sehen, dass "$*" immer ein einziges Argument bedeutet, und "[email protected]" enthält so viele Argumente, wie das Skript hatte. "$ @" ist ein spezielles Token, das bedeutet "jedes einzelne Argument in Anführungszeichen setzen". So a "b c" d wird (oder bleibt) "a" "b c" "d" anstelle von "a b c d" ("$*") oder "a" "b" "c" "d" ([email protected] oder $*).

Außerdem würde ich diese schöne Lesung zu dem Thema empfehlen:

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

Verwandte Themen