Bash中[和[[有什麼區別? [重複]

本文翻譯自:What's the difference between [ and [[ in Bash? [duplicate]

This question already has an answer here: 這個問題已經在這裏有了答案:

I looked at bash man page and the [[ says it uses Conditional Expressions. 我看着bash手冊頁, [[說它使用條件表達式。 Then I looked at Conditional Expressions section and it lists the same operators as test (and [ ). 然後,我查看了條件表達式部分,其中列出了與test (和[ )相同的運算符。

So I wonder, what is the difference between [ and [[ in Bash? 所以我想知道,在Bash中[[[之間有什麼區別?


#1樓

參考:https://stackoom.com/question/ENkG/Bash中-和-有什麼區別-重複


#2樓

The most important difference will be the clarity of your code. 最重要的區別是代碼的清晰度 Yes, yes, what's been said above is true, but [[ ]] brings your code in line with what you would expect in high level languages, especially in regards to AND ( && ), OR ( || ), and NOT ( ! ) operators. 是的,是的,上面所說的是正確的,但是[[]]使您的代碼符合您在高級語言中的期望,尤其是關於AND( && ),OR( || )和NOT( )運算符。 Thus, when you move between systems and languages you will be able to interpret script faster which makes your life easier. 因此,當您在系統和語言之間切換時,您將能夠更快地解釋腳本,從而使您的生活更輕鬆。 Get the nitty gritty from a good UNIX/Linux reference. 從良好的UNIX / Linux參考中獲取實質內容。 You may find some of the nitty gritty to be useful in certain circumstances, but you will always appreciate clear code! 在某些情況下,您可能會發現一些棘手的內容很有用,但是您將始終喜歡清晰的代碼! Which script fragment would you rather read? 您希望閱讀哪個腳本片段? Even out of context, the first choice is easier to read and understand. 即使沒有上下文,首選也更易於閱讀和理解。


if [[ -d $newDir && -n $(echo $newDir | grep "^${webRootParent}") && -n $(echo $newDir | grep '/$') ]]; then ...

or 要麼

if [ -d "$newDir" -a -n "$(echo "$newDir" | grep "^${webRootParent}")" -a -n "$(echo "$newDir" | grep '/$')" ]; then ...

#3樓

在bash中,與[[[防止變量值的單詞分割相反。


#4樓

[[ is bash's improvement to the [ command. [[是bash對[命令的改進。 It has several enhancements that make it a better choice if you write scripts that target bash. 它具有多項增強功能,如果編寫針對bash的腳本,則使其成爲更好的選擇。 My favorites are: 我的最愛是:

  1. It is a syntactical feature of the shell, so it has some special behavior that [ doesn't have. 它是Shell的語法功能,因此它具有[沒有的特殊行爲。 You no longer have to quote variables like mad because [[ handles empty strings and strings with whitespace more intuitively. 您不再需要引用mad之類的變量,因爲[[更直觀地處理空字符串和帶空格的字符串。 For example, with [ you have to write 例如,使用[

     if [ -f "$file" ] 

    to correctly handle empty strings or file names with spaces in them. 正確處理空字符串或帶有空格的文件名。 With [[ the quotes are unnecessary: 使用[[引號是不必要的:

     if [[ -f $file ]] 
  2. Because it is a syntactical feature, it lets you use && and || 由於它是一種語法功能,因此您可以使用&&|| operators for boolean tests and < and > for string comparisons. 用於布爾測試的運算符,用於字符串比較的<>運算符。 [ cannot do this because it is a regular command and && , || [無法執行此操作,因爲它是常規命令和&&|| , < , and > are not passed to regular commands as command-line arguments. <>不會作爲命令行參數傳遞給常規命令。

  3. It has a wonderful =~ operator for doing regular expression matches. 它有一個很棒的=~運算符,用於進行正則表達式匹配。 With [ you might write 使用[您可能會寫

     if [ "$answer" = y -o "$answer" = yes ] 

    With [[ you can write this as 使用[[您可以將其寫爲

     if [[ $answer =~ ^y(es)?$ ]] 

    It even lets you access the captured groups which it stores in BASH_REMATCH . 它甚至允許您訪問存儲在BASH_REMATCH的捕獲的組。 For instance, ${BASH_REMATCH[1]} would be "es" if you typed a full "yes" above. 例如,如果您在上方鍵入完整的“是”,則${BASH_REMATCH[1]}將爲“ es”。

  4. You get pattern matching aka globbing for free. 您可以免費獲得模式匹配(也稱爲“ globbing”)。 Maybe you're less strict about how to type yes. 也許您對如何輸入yes不太嚴格。 Maybe you're okay if the user types y-anything. 如果用戶鍵入y-anything,則可能沒問題。 Got you covered: 得到了覆蓋:

     if [[ $ANSWER = y* ]] 

Keep in mind that it is a bash extension, so if you are writing sh-compatible scripts then you need to stick with [ . 請記住,它是bash擴展,因此,如果要編寫與sh兼容的腳本,則需要堅持使用[ Make sure you have the #!/bin/bash shebang line for your script if you use double brackets. 如果使用雙括號,請確保腳本具有#!/bin/bash shebang行。

See also 也可以看看


#5樓

  • [ is the same as the test builtin, and works like the test binary (man test) [與內置test相同,並且類似於test二進制文件(人工測試)
    • works about the same as [ in all the other sh-based shells in many UNIX-like environments 在許多類似UNIX的環境中,與[在所有其他基於sh的shell中幾乎一樣
    • only supports a single condition. 僅支持一個條件。 Multiple tests with the bash && and || 使用bash &&||多次測試 operators must be in separate brackets. 操作員必須放在單獨的括號中。
    • doesn't natively support a 'not' operator. 本機不支持“非”運算符。 To invert a condition, use a ! 要反轉條件,請使用! outside the first bracket to use the shell's facility for inverting command return values. 在第一個括號之外,以使用Shell的功能來反轉命令返回值。
    • == and != are literal string comparisons ==!=是文字字符串比較
  • [[ is a bash [[是一個bash
    • is bash-specific, though others shells may have implemented similar constructs. 是bash特定的,儘管其他shell可能已經實現了類似的構造。 Don't expect it in an old-school UNIX sh. 不要在老式UNIX sh中期望它。
    • == and != apply bash pattern matching rules, see "Pattern Matching" in man bash ==!=應用bash模式匹配規則,請參見man bash “模式匹配”
    • has a =~ regex match operator 有一個=~正則表達式匹配運算符
    • allows use of parentheses and the ! 允許使用括號和! , && , and || &&|| logical operators within the brackets to combine subexpressions 方括號內的邏輯運算符用於組合子表達式

Aside from that, they're pretty similar -- most individual tests work identically between them, things only get interesting when you need to combine different tests with logical AND/OR/NOT operations. 除此之外,它們非常相似-大多數單獨的測試在它們之間的工作方式相同,只有當您需要將不同的測試與邏輯AND / OR / NOT運算結合在一起時,事情纔會變得有趣。

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