2010-07-22 74 views
14

UNIX'te [email protected] ve $* arasındaki fark nedir? Bir senaryoda yankılandığında, her ikisi de aynı çıktıyı üretir.

+0

bakınız [Bir bash komut argümanlar üzerinde yineleme yöntemi] (http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script/256225# 256.225). Bu cevap, $ @ 've' $ * 'arasındaki farklılıkları (soru olmasa bile) açıkça kapsar. En önemli fark, teklif altındaki davranışlarda - "$ *" ve "$ @" arasındaki farktır. –

cevap

5

farklardan biri de çıkışını IFS değişkeni nasıl ele içindedir.

#!/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]" 

böyle bu çalıştırırsanız: ./demo abc def ghi, bu çıktıyı almak:

(yalnızca) "alıntı asterisk" satırı IFS ardından gelen her "kelime" arasında bir X gösteren Bildirimi o
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 

"X" olarak değiştirildi. IFS'un değeri birden çok karakter içeriyorsa, bu amaçla yalnızca ilk karakter kullanılır. Bu özellik

diğer diziler için de kullanılabilir:

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

Lütfen Özel Parametreler altındaki bash man sayfasına bakın.

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

O "[email protected]" yerine $* kullanmak güvenlidir. Çoklu komut dizilerini bir kabuk komut dosyasına argüman olarak kullandığınızda, yalnızca her bir bağımsız değişkeni ayrı bir argüman olarak yorumlayan "[email protected]" olur.

Yukarıdaki çıktı öneriyorsa, $* kullanırsanız, kabuk bağımsız değişkenlerin sayısını sayar.

İlgili konular