我查看了bash的man页面,发现[[
使用条件表达式。然后我查看了条件表达式部分,发现它列出了与test
(以及[
)相同的运算符。
所以我想知道,在Bash中[
和[[
之间有什么区别?
我查看了bash的man页面,发现[[
使用条件表达式。然后我查看了条件表达式部分,发现它列出了与test
(以及[
)相同的运算符。
所以我想知道,在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* ]]
[
。如果使用双括号,请确保您的脚本有#!/bin/bash
shebang行。
[
与test
内置命令功能相同,并像test
二进制文件一样工作(参见"man test")。
[
基本上是一样的。&&
和||
运算符进行多个测试必须分别放在不同的括号内。!
来使用Shell的反转命令返回值的功能。==
和!=
是字面字符串比较。[[
是bash的特性:
==
和!=
应用bash模式匹配规则,请参见"Pattern Matching" in man bash
=~
正则表达式匹配运算符!
、&&
和||
逻辑运算符来组合子表达式。除此之外,它们非常相似--它们之间的大多数单个测试工作方式是相同的,只有在需要使用逻辑AND/OR/NOT操作组合不同的测试时才会变得有趣。
最重要的区别将是您的代码的清晰度。是的,上面所说的是真的,但[[ ]]使您的代码与高级语言中的预期相一致,特别是关于AND (&&)、OR (||)和NOT (!)运算符。因此,当您在不同系统和语言之间移动时,您将能够更快地解释脚本,这使您的生活更轻松。从一个好的UNIX/Linux参考手册中了解细节。在某些情况下,您可能会发现其中一些细节很有用,但您总是会欣赏清晰的代码!您更喜欢阅读哪个脚本片段?即使是超出上下文,第一个选择也更容易阅读和理解。
if [[ -d $newDir && -n $(echo $newDir | grep "^${webRootParent}") && -n $(echo $newDir | grep '/$') ]]; then ...
或者if [ -d "$newDir" -a -n "$(echo "$newDir" | grep "^${webRootParent}")" -a -n "$(echo "$newDir" | grep '/$')" ]; then ...
[
是一个命令,而[[
是一个语法元素(“保留字”)。因此解析不同。” - U. Windl[[ ]]
是 let
的快捷方式。归根结底,确实存在语法上的差异,但对于普通人来说,易读性和可维护性才是最重要的。因此,这就是为什么我说“最重要”,而不是“唯一”或“按照书本规定”的区别。 - Anthony Rutledge(( ))
,类似于 let
。但这仅适用于算术计算,而不仅仅是布尔逻辑。 - Johan Boulé(( ))
进行算术运算。 - Anthony Rutledge[
不同的是,[[
防止变量值的单词拆分。
-n
、-z
、-ge
、-eq
、-d
等 man 手册的人,答案是man test
。 - Gabriel Staples