When do we need curly braces around shell variables?

后端 未结 7 1225
春和景丽
春和景丽 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:10

    You use {} for grouping. The braces are required to dereference array elements. Example:

    dir=(*)           # store the contents of the directory into an array
    echo "${dir[0]}"  # get the first entry.
    echo "$dir[0]"    # incorrect
    

提交回复
热议问题