When do we need curly braces around shell variables?

后端 未结 7 1100
春和景丽
春和景丽 2020-11-22 01:39

In shell scripts, when do we use {} when expanding variables?

For example, I have seen the following:

var=10        # Declare variable

         


        
7条回答
  •  被撕碎了的回忆
    2020-11-22 02:01

    You are also able to do some text manipulation inside the braces:

    STRING="./folder/subfolder/file.txt"
    echo ${STRING} ${STRING%/*/*}
    

    Result:

    ./folder/subfolder/file.txt ./folder
    

    or

    STRING="This is a string"
    echo ${STRING// /_}
    

    Result:

    This_is_a_string
    

    You are right in "regular variables" are not needed... But it is more helpful for the debugging and to read a script.

提交回复
热议问题