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

后端 未结 7 776
心在旅途
心在旅途 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:33

    I just wanted to add these from TLDP:

    ~:$ 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
    

提交回复
热议问题