2010-07-22 24 views
14

Qual è la differenza tra [email protected] e $* in UNIX? Quando vengono riprodotti in uno script, entrambi sembrano produrre lo stesso risultato.

+0

Sede [Come iterare gli argomenti in uno script bash] (http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script/256225# 256225). Quella risposta copre esplicitamente le differenze tra '$ @' e '$ *' (anche se la domanda non lo fa). La differenza cruciale sta nel comportamento tra virgolette - la differenza tra "$ *" e "$ @" '. –

risposta

5

Una differenza è nel modo in cui gestiscono la variabile IFS in uscita.

#!/bin/sh 
echo "unquoted asterisk " $* 
echo "quoted asterisk $*" 
echo "unquoted at " [email protected] 
echo "quoted at [email protected]" 
IFS="X" 
echo "IFS is now $IFS" 
echo "unquoted asterisk " $* 
echo "quoted asterisk $*" 
echo "unquoted at " [email protected] 
echo "quoted at [email protected]" 

Se si esegue questo in questo modo: ./demo abc def ghi, si ottiene questa uscita:

unquoted asterisk abc def ghi 
quoted asterisk abc def ghi 
unquoted at abc def ghi 
quoted at abc def ghi 
IFS is now X 
unquoted asterisk abc def ghi 
quoted asterisk abcXdefXghi 
unquoted at abc def ghi 
quoted at abc def ghi 

Si noti che (solo) la linea "asterisco citato" mostra una X tra ogni "parola" dopo IFS è cambiato in "X". Se il valore di IFS contiene più caratteri, solo il primo carattere viene utilizzato per questo scopo.

Questa funzione può essere utilizzata anche per altri array:

$ array=(123 456 789) 
$ saveIFS=$IFS; IFS="|" 
$ echo "${array[*]}" 
123|456|789 
$ IFS=$saveIFS 
13

Vedere la pagina di manuale di bash in Parametri speciali.

Special Parameters 
     The shell treats several parameters specially. These parameters may 
     only be referenced; assignment to them is not allowed. 
     *  Expands to the positional parameters, starting from one. When 
       the expansion occurs within double quotes, it expands to a sin‐ 
       gle word with the value of each parameter separated by the first 
       character of the IFS special variable. That is, "$*" is equiva‐ 
       lent to "$1c$2c...", where c is the first character of the value 
       of the IFS variable. If IFS is unset, the parameters are sepa‐ 
       rated by spaces. If IFS is null, the parameters are joined 
       without intervening separators. 
     @  Expands to the positional parameters, starting from one. When 
       the expansion occurs within double quotes, each parameter 
       expands to a separate word. That is, "[email protected]" is equivalent to "$1" 
       "$2" ... If the double-quoted expansion occurs within a word, 
       the expansion of the first parameter is joined with the begin‐ 
       ning part of the original word, and the expansion of the last 
       parameter is joined with the last part of the original word. 
       When there are no positional parameters, "[email protected]" and [email protected] expand to 
       nothing (i.e., they are removed). 
2

E 'più sicuro di usare "[email protected]" invece di $*. Quando si utilizzano le stringhe composte da più termini come argomenti ad uno script di shell, è solo "[email protected]" che interpreta ogni argomento citato come un argomento separato.

Come l'output sopra riportato suggerisce, se si utilizza $*, la shell esegue un conteggio errato degli argomenti.

Problemi correlati