我查看了bash手册页,[[说它使用条件表达式。然后我查看了条件表达式部分,它列出了与test(和[)相同的操作符。

所以我想知道,Bash中的[和[[有什么区别?


在bash中,与[相反,[[防止变量值的分词。


[[是bash对[命令的改进。如果您编写以bash为目标的脚本,它有几个增强功能,使其成为更好的选择。我最喜欢的是:

It is a syntactical feature of the shell, so it has some special behavior that [ doesn't have. You no longer have to quote variables like mad because [[ handles empty strings and strings with whitespace more intuitively. 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 ]] 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. 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. For instance, ${BASH_REMATCH[1]} would be "es" if you typed a full "yes" above. You get pattern matching aka globbing for free. Maybe you're less strict about how to type yes. Maybe you're okay if the user types y-anything. Got you covered: if [[ $ANSWER = y* ]]

请记住,它是一个bash扩展,所以如果您正在编写与sh兼容的脚本,那么您需要坚持使用[。确保你有#!/bin/bash shebang行,如果你使用双括号。

另请参阅

Bash常见问题-“test,[和[[之间的区别是什么?” Bash实践- Bash测试 服务器故障- bash中双括号和单括号之间的区别是什么?


[ is the same as the test builtin, and works like the test binary (man test) works about the same as [ in all the other sh-based shells in many UNIX-like environments only supports a single condition. Multiple tests with the bash && and || 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. == and != are literal string comparisons [[ is a bash is bash-specific, though others shells may have implemented similar constructs. Don't expect it in an old-school UNIX sh. == and != apply bash pattern matching rules, see "Pattern Matching" in man bash has a =~ regex match operator allows use of parentheses and the !, &&, and || logical operators within the brackets to combine subexpressions

除此之外,它们非常相似——大多数单独的测试在它们之间的工作方式相同,只有当您需要将不同的测试与逻辑AND/OR/NOT操作结合在一起时,事情才会变得有趣。


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. 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. 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 ...