2013-03-24 14 views
5

nel mio script.sh:

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

durante l'esecuzione di esso:

source script.sh a b c d e f g 

ottengo:

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

Qual è la differenza tra [email protected] e $*?

+0

@mat perché Google non ha trovato tutti gli altri duplicati? – 0x90

+1

@ 0x90: Google non indice "punteggiatura", solo parole; prova [symbolhound] (http://symbolhound.com/?q=%24%40+%24%2A) per cose come questa. –

risposta

8

Non ci sono alcuna differenza tra $* e [email protected], ma c'è una differenza tra "[email protected]" e "$*".

$ 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 

Abbiamo dato tre argomenti allo script (a, b c e d), ma in "$ *" tutti sono stati fusi in un unico argomento 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 

Si può vedere qui, che "$*" significa sempre un solo argomento, e "[email protected]" contiene tanti argomenti, come la sceneggiatura aveva. "$ @" è un token speciale che significa "incapsulare ogni singolo argomento tra virgolette". Quindi a "b c" d diventa (o meglio rimane) "a" "b c" "d" anziché "a b c d" ("$*") o "a" "b" "c" "d" ([email protected] o $*).

Inoltre, mi sento di raccomandare questa bella lettura sul tema:

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

Problemi correlati