Shell中[和[[的異同


轉載:http://blog.csdn.net/ysdaniel/article/details/7905818

整理自:http://bbs.chinaunix.net/thread-278896-2-1.html


1.     概念上來說

"[[",是關鍵字,許多shell(如ash bsh)並不支持這種方式。ksh, bash(據說從2.02起引入對[[的支持)等支持。
"["是一條命令, 與test等價,大多數shell都支持。在現代的大多數sh實現中,"["與"test"是內部(builtin)命令,換句話說執行"test"/"["時不會調

2.     相同:二者都支持算術比較和字符串比較表達式(具體使用可能有點不同)

(1)"-gt", "-lt"是算術比較操作符,用於比較整數的大小。
(2)">", "<"是字符串比較操作符,用於比較字符串的大小,使用字典順序,與當前的locale有關。

(3).關於字符串比較。[...]、``.``.``.``中都可以對字符串進行比較,比較的順序是"字典順序"。對ascii字符來講,碼錶中排列在前的較小,如A<B,A<a, 1<2。再強調一次,這裏只要用了"<"、">",就表示是字符串比較,那麼9 > 100爲真,因爲這實際上等價於‘9’ > ‘100’,9在碼錶中排在1後面,所以字符串"9"大於字符串"100"。只要搞清楚了何時是算術比較,何時是串比較,一般就不會出錯了。

(4)建議在使用數值比較的時候,使用let,(())命令,否則容易出錯;

2.1  “[“用法

$ [ 2 -lt 10 ]&&echo true&&echo false

true

$  [ 2 -gt 10 ]&&echo true||echo false

false

$  [ 2\< 10 ]&&echo true||echo false  #you should use "\<"

false

$  [ 2 \> 10 ]&&echo true||echo false  #you should use "\>"

true

2.2     “[[“用法

$  ` 2 -gt 10 `&&echo true||echo false

false

$  ` 2 -lt 10 `&&echo true||echo false

true

$  [[ 2 < 10 ]]&&echo true||echo false

false

$  [[ 2 > 10 ]]&&echo true||echo false

true


3.        相同:都支持簡單的模式匹配

這裏的模式匹配要簡單得多,類似文件名的統配符的擴展規則。還要注意等號右端的模式不能用引號括起,使用引用關閉了某些元字符的特殊功能

3.1  “[“用法

$ [ test = test ]&&echo true||echo false  #normal compare

true

$ [ test = t*t ]&&echo true||echo false  #pattern match.

true

$ [ test = t..t ]&&echo true||echo false  #not match.

false

$ [ test = t??t ]&&echo true||echo false  #note that "?", not "." stands for one single character here

true

$ [ test = "t??t" ]&&echo true||echo false #alert: don't quote the pattern,使用引用關閉了?的特殊功能

false

3.2  “[[“用法

$ [[ test = test ]]&&echo true||echo false  #normal compare

true

$ [[ test = t*t ]]&&echo true||echo false  #pattern match.

true

$ [[ test = t..t ]]&&echo true||echo false  #not match.

false

$ [[ test = t??t ]]&&echo true||echo false  #note that "?", not "." stands for one single character here

true

$ [[ test = "t??t" ]]&&echo true||echo false # alert: don't quote the pattern,使用引用關閉了?的特殊功能

false


4.        不同點

4.1     邏輯與和邏輯或

1"[":邏輯與:"-a";邏輯或:"-o"

2"[[":邏輯與:"&&";邏輯或:"||"

$ [[ 1 < 2 && b > a ]]&&echo true||echo false

true

$ [[ 1 < 2 -a b > a ]]&&echo true||echo false

bash: syntax error in conditional expression

bash: syntax error near `-a'

$ [ 1 < 2 -a b > a ]&&echo true||echo false

true

$ [ 1 < 2 && b > a ]&&echo true||echo false  #wrong syntax

bash: [: missing `]'

false

$ [ 1 < 2 \&\& b > a ]&&echo true||echo false  #aslo wrong

bash: [: &&: binary operator expected

false


4.2     命令行參數

(1)[ ... ]爲shell命令,所以在其中的表達式應是它的命令行參數,所以串比較操作符">" 與"<"必須轉義,否則就變成IO重定向了;

(2)由於"[["是關鍵字,不會做命令行擴展,所以在[[中"<"與">"不需轉義,但是相對的語法就稍嚴格些。例如在[ ... ]中可以用引號括起操作符,因爲在做命令行擴展時會去掉這些引號,而在` `.``.``.` `則不允許這樣做;

$ [ "-z" "" ]&&echo true||echo false

true

$ [ -z "" ]&&echo true||echo false

true

$ [[ "-z" "" ]]&&echo true||echo false

bash: conditional binary operator expected

bash: syntax error near `""'

$ [[ -z "" ]]&&echo true||echo false

true


4.3  ` `.``.``.` `進行算術擴展,而[ ... ]不做

$ [[ 99+1 -eq 100 ]]&&echo true||echo false

true

$ [ 99+1 -eq 100 ]&&echo true||echo false

bash: [: 99+1: integer expression expected

false

$ [ $((99+1)) -eq 100 ]&&echo true||echo false

true


4.4  正則表達式匹配"=~"

regular expression match. This operator was introduced with version 3 of Bash.
The =~ Regular Expression matching operator within a double brackets test expression.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章