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运算结合在一起时,事情才会变得有趣。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值