关于bash:如何使用双括号或单括号、括号、花括号

How to use double or single brackets, parentheses, curly braces

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


在bash中,test[是内置的。

双支架,使额外的功能。例如,你可以使用&&||-a-o=~算子的正则表达式匹配。

此外,delimiting的牙套,该变量的名称是用于参数展开的东西:你可以像操作系统

  • 截断变量的内容

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

  • sed结构类似于make

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

  • 使用默认值

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

  • 和多更多

另外,支撑expansions创建列表,这是在typically strings中迭代循环:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ 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。

我感谢我们expansions reminding gboffi是支撑。

双parentheses是用于算术操作:

1
2
3
4
5
6
7
((a++))

((meaning = 42))

for ((i=0; i<10; i++))

echo $((a + b + (14 * c)))

他们使你omit美元符号的整数变量和数组,包括在线和readability空间是在运营商。

单阵列也被用于例如指数:

1
2
3
array[4]="hello"

element=${array[index]}

卷曲的支撑是必要的(几乎全是(?)数组引用的对侧手。

我reminded ephemient的评论也被用于parentheses壳层。这就是他们用来创建数组。

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


  • 单支架([)通常实际上调用程序或man [[man test的更多信息。例子:

    1
    2
    3
    $ VARIABLE=abcdef
    $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
    yes
  • 在双括号([[)做同样的事(基本上)的单支架,但内建的是bash。

    1
    2
    3
    $ VARIABLE=abcdef
    $ if [[ $VARIABLE == 123456 ]] ; then echo yes ; else echo no ; fi
    no
  • parentheses(())是用来创建一个壳层。例如:

    1
    2
    3
    4
    5
    6
    $ pwd
    /home/user
    $ (cd /tmp; pwd)
    /tmp
    $ pwd
    /home/user

    你可以看到的,允许你执行操作的计算没有影响当前shell环境。

  • (一)支撑({}unambiguously)是用来确定的变量。例子:

    1
    2
    3
    4
    5
    6
    7
    $ VARIABLE=abcdef
    $ echo Variable: $VARIABLE
    Variable: abcdef
    $ echo Variable: $VARIABLE123456
    Variable:
    $ echo Variable: ${VARIABLE}123456
    Variable: abcdef123456

    (二)支撑也被用来运行在当前的shell命令序列上下文。

    1
    2
    3
    4
    5
    6
    $ { date; top -b -n1 | head ; } >logfile
    # 'date' and 'top' output are concatenated,
    # could be useful sometimes to hunt for a top loader )

    $ { date; make 2>&1; date; } | tee logfile
    # now we can calculate the duration of a build from the logfile
  • 差分有一个微妙的句法与( )(ESS),但基本上,Bash参考);;命令后在最后的分号是必须的牙套,牙套和{}surrounded,必须通过空间。


    例如

    1
    2
    3
    4
    5
    if [ CONDITION ]    Test construct  
    if [[ CONDITION ]]  Extended test construct  
    Array[1]=element1   Array initialization  
    [a-z]               Range of characters within a Regular Expression
    $[ expression ]     A non-standard & obsolete version of $(( expression )) [1]

    〔1〕http://wiki.bash-hackers.org /脚本/过时

    卷曲支架

    1
    2
    3
    4
    5
    6
    ${variable}                             Parameter substitution  
    ${!variable}                            Indirect variable reference  
    { command1; command2; . . . commandN; } Block of code  
    {string1,string2,string3,...}           Brace expansion  
    {a..z}                                  Extended brace expansion  
    {}                                      Text replacement, after find and xargs

    parentheses

    1
    2
    3
    4
    5
    ( command1; command2 )             Command group executed within a subshell  
    Array=(element1 element2 element3) Array initialization  
    result=$(COMMAND)                  Command substitution, new style  
    >(COMMAND)                         Process substitution  
    <(COMMAND)                         Process substitution

    双parentheses

    1
    2
    3
    4
    5
    (( var = 78 ))            Integer arithmetic  
    var=$(( 20 + 5 ))         Integer arithmetic, with variable assignment  
    (( var++ ))               C-style variable increment  
    (( var-- ))               C-style variable decrement  
    (( var0 = var1<98?9:21 )) C-style ternary operation


    我只是想添加这些来自tldp:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    ~:$ echo $SHELL
    /bin/bash

    ~:$ echo ${#SHELL}
    9

    ~:$ ARRAY=(one two three)

    ~:$ echo ${#ARRAY}
    3

    ~:$ echo ${TEST:-test}
    test

    ~:$ echo $TEST


    ~:$ export TEST=a_string

    ~:$ echo ${TEST:-test}
    a_string

    ~:$ echo ${TEST2:-$TEST}
    a_string

    ~:$ echo $TEST2


    ~:$ echo ${TEST2:=$TEST}
    a_string

    ~:$ echo $TEST2
    a_string

    ~:$ export STRING="thisisaverylongname"

    ~:$ echo ${STRING:4}
    isaverylongname

    ~:$ echo ${STRING:6:5}
    avery

    ~:$ echo ${ARRAY[*]}
    one two one three one four

    ~:$ echo ${ARRAY[*]#one}
    two three four

    ~:$ echo ${ARRAY[*]#t}
    one wo one hree one four

    ~:$ echo ${ARRAY[*]#t*}
    one wo one hree one four

    ~:$ echo ${ARRAY[*]##t*}
    one one one four

    ~:$ echo $STRING
    thisisaverylongname

    ~:$ echo ${STRING%name}
    thisisaverylong

    ~:$ echo ${STRING/name/string}
    thisisaverylongstring


    测试之间的差异的解释是,[与[在bashfaq在伟大的细节。

    To cut a long story short: test implements the old, portable syntax of
    the command. In almost all shells (the oldest Bourne shells are the
    exception), [ is a synonym for test (but requires a final argument of
    ]). Although all modern shells have built-in implementations of [,
    there usually still is an external executable of that name, e.g.
    /bin/[.

    [[ is a new improved version of it, which is a keyword, not a program.
    This has beneficial effects on the ease of use, as shown below. [[ is
    understood by KornShell and BASH (e.g. 2.03), but not by the older
    POSIX or BourneShell.

    的结论:

    When should the new test command [[ be used, and when the old one [?
    If portability to the BourneShell is a concern, the old syntax should
    be used. If on the other hand the script requires BASH or KornShell,
    the new syntax is much more flexible.


    在函数定义parentheses

    parentheses ()是被用于函数定义:

    1
    function_name () { command1 ; command2 ; }

    这是你逃避的原因,甚至在parentheses命令参数:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ echo (
    bash: syntax error near unexpected token `newline'

    $ echo \(
    (

    $ echo () { command echo The command echo was redefined. ; }
    $ echo anything
    The command echo was redefined.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Truncate the contents of a variable

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

    Make substitutions similar to sed

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

    Use a default value

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