When do we need curly braces around shell variables?

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

For example, I have seen the following:

var=10        # Declare variable

echo "${var}" # One use of the variable
echo "$var"   # Another use of the variable

Is there a significant difference, or is it just style? Is one preferred over the other?


In this particular example, it makes no difference. However, the {} in ${} are useful if you want to expand the variable foo in the string

"${foo}bar"

since "$foobar" would instead expand foobar .

Curly braces are also unconditionally required when:

  • expanding array elements, as in ${array[42]}
  • using parameter expansion operations, as in ${filename%.*} (remove extension)
  • expanding positional parameters beyond 9: "$8 $9 ${10} ${11}"
  • Doing this everywhere, instead of just in potentially ambiguous cases, can be considered good programming practice. This is both for consistency and to avoid surprises like $foo_$bar.jpg , where it's not visually obvious that the underscore becomes part of the variable name.


    Variables are declared and assigned without $ and without {} . You have to use

    var=10
    

    to assign. In order to read from the variable (in other words, 'expand' the variable), you must use $ .

    $var      # use the variable
    ${var}    # same as above
    ${var}bar # expand var, and append "bar" too
    $varbar   # same as ${varbar}, i.e expand a variable called varbar, if it exists.
    

    This has confused me sometimes - in other languages we refer to the variable in the same way, regardless of whether it's on the left or right of an assignment. But shell-scripting is different, $var=10 doesn't do what you might think it does!


    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
    
    链接地址: http://www.djcxy.com/p/4956.html

    上一篇: 如何检查变量是否在Bash中设置?

    下一篇: 我们何时需要围绕shell变量使用花括号?