如何使用双括号或单括号、圆括号、花括号 [英] How to use double or single brackets, parentheses, curly braces

查看:234
本文介绍了如何使用双括号或单括号、圆括号、花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Bash 中括号、圆括号、花括号的使用以及它们的双重或单一形式之间的区别感到困惑.有明确的解释吗?

I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?

推荐答案

在 Bash 中,test[ 是 shell 内置的.

In Bash, test and [ are shell builtins.

双括号,它是一个 shell 关键字,可启用附加功能.例如,您可以使用 &&|| 而不是 -a-o ,还有正则表达式匹配运算符 =~.

The double bracket, which is a shell keyword, enables additional functionality. For example, you can use && and || instead of -a and -o and there's a regular expression matching operator =~.

另外,在一个简单的测试中,双方括号的评估似乎比单方括号快很多.

Also, in a simple test, double square brackets seem to evaluate quite a lot quicker than single ones.

$ time for ((i=0; i<10000000; i++)); do [[ "$i" = 1000 ]]; done

real    0m24.548s
user    0m24.337s
sys 0m0.036s
$ time for ((i=0; i<10000000; i++)); do [ "$i" = 1000 ]; done

real    0m33.478s
user    0m33.478s
sys 0m0.000s

大括号除了用于分隔变量名外还用于 参数扩展,因此您可以执行以下操作:

The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like:

  • 截断变量的内容

  • Truncate the contents of a variable

$ var="abcde"; echo ${var%d*}
abc

  • 进行类似于 sed

    $ var="abcde"; echo ${var/de/12}
    abc12
    

  • 使用默认值

  • Use a default value

    $ default="hello"; unset var; echo ${var:-$default}
    hello
    

  • 还有几个

  • and several more

    此外,大括号扩展创建通常在循环中迭代的字符串列表:

    Also, brace expansions create lists of strings which are typically iterated over in loops:

    $ echo f{oo,ee,a}d
    food feed fad
    
    $ mv error.log{,.OLD}
    (error.log is renamed to error.log.OLD because the brace expression
    expands to "mv error.log error.log.OLD")
    
    $ for num in {000..2}; do echo "$num"; done
    000
    001
    002
    
    $ echo {00..8..2}
    00 02 04 06 08
    
    $ echo {D..T..4}
    D H L P T
    

    请注意,在 Bash 4 之前,前导零和增量功能不可用.

    Note that the leading zero and increment features weren't available before Bash 4.

    感谢 gboffi 提醒我大括号扩展.

    Thanks to gboffi for reminding me about brace expansions.

    双括号用于算术运算:

    ((a++))
    
    ((meaning = 42))
    
    for ((i=0; i<10; i++))
    
    echo $((a + b + (14 * c)))
    

    并且它们使您能够省略整数和数组变量上的美元符号,并在运算符周围包含空格以提高可读性.

    and they enable you to omit the dollar signs on integer and array variables and include spaces around operators for readability.

    单括号也用于数组索引:

    array[4]="hello"
    
    element=${array[index]}
    

    右侧的(大多数/所有?)数组引用需要花括号.

    Curly brace are required for (most/all?) array references on the right hand side.

    ephemient 的 评论提醒我括号也用于子shell.并且它们用于创建数组.

    ephemient's comment reminded me that parentheses are also used for subshells. And that they are used to create arrays.

    array=(1 2 3)
    echo ${array[1]}
    2
    

    这篇关于如何使用双括号或单括号、圆括号、花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆