2010-07-22 41 views
14

Sự khác nhau giữa [email protected]$* trong UNIX là gì? Khi được lặp lại trong một kịch bản, cả hai dường như tạo ra cùng một đầu ra.

+0

Xem [Cách lặp qua các đối số trong tập lệnh bash] (http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script/256225# 256225). Câu trả lời đó trình bày rõ ràng sự khác biệt giữa '$ @' và '$ *' (ngay cả khi câu hỏi không có). Sự khác biệt quan trọng là trong hành vi theo dấu ngoặc kép - sự khác biệt giữa '" $ * "' và '" $ @ "'. –

Trả lời

5

Một điểm khác biệt là cách họ xử lý biến IFS trên đầu ra.

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

Nếu bạn chạy như thế này: ./demo abc def ghi, bạn sẽ có được kết quả này:

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 

Chú ý rằng (chỉ) là "trích dẫn dấu" dòng hiển thị X giữa mỗi "chữ" sau IFS là đổi thành "X". Nếu giá trị IFS chứa nhiều ký tự, chỉ ký tự đầu tiên được sử dụng cho mục đích này.

Tính năng này cũng có thể được sử dụng cho các mảng khác:

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

Vui lòng xem trang bash man trong Tham số đặc biệt.

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

Đó là an toàn hơn để sử dụng "[email protected]" thay vì $*. Khi bạn sử dụng chuỗi nhiều từ làm đối số cho tập lệnh hệ vỏ, chỉ có "[email protected]" diễn giải từng đối số được trích dẫn dưới dạng đối số riêng biệt.

Khi đầu ra ở trên gợi ý, nếu bạn sử dụng $*, vỏ sẽ làm sai số đối số.

Các vấn đề liên quan