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

后端 未结 7 915
心在旅途
心在旅途 2020-11-21 23:10

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?

7条回答
  •  轮回少年
    2020-11-21 23:31

    Brackets

    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/scripting/obsolete

    Curly Braces

    ${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

    ( 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 
    

    Double Parentheses

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

提交回复
热议问题