What's the difference between ${varname} and $varname in a shell scripts

后端 未结 3 745
忘掉有多难
忘掉有多难 2020-12-02 02:00

I have a simple question but I wonder what is the difference between ${varname} and $varname ?

I use both but I don\'t see any difference w

相关标签:
3条回答
  • 2020-12-02 02:17

    The distinction becomes important when something follows the variable:

    text="House"
    plural="${text}s"
    

    Without the braces, the shell would see texts as variable name which wouldn't work.

    The braces are also necessary when you use the extended syntax to specify defaults (${name-default}), display errors when undefined (${name?error}), or pattern substitution (see this article for other patterns; it's for BASH but most work for KSH as well)

    > echo $name-default
    -default
    > echo ${name-default}
    default
    

    Related:

    • Parameter Substitution in Korn-/POSIX-Shell
    0 讨论(0)
  • 2020-12-02 02:28

    Using {} in variable names helps get rid of ambiguity while performing variable expansion.

    Consider two variables var and varname. Lets see you wanted to append the string name to the variable var. You can't say $varname because that would result in the expansion of the variable varname. However, saying ${var}name would help you achieve the desired result.

    $ var="This is var variable."
    $ varname="This is varname variable."
    $ echo $varname
    This is varname variable.
    $ echo ${var}name
    This is var variable.name
    

    Braces are also required when accessing any element of an array.

    $ a=( foo bar baz )       # Declare an array
    $ echo $a[0]              # Accessing first element -- INCORRECT
    foo[0]
    $ echo ${a[0]}            # Accessing first element -- CORRECT
    foo
    

    Quoting from info bash:

       Any  element  of  an  array may be referenced using ${name[subscript]}.
       The braces are required to avoid conflicts with pathname expansion.
    
    0 讨论(0)
  • 2020-12-02 02:43

    They are the same in a basic case, but using ${varname} gives more control and ability to work with the variable. It also skips edge cases in which it can create confusion. And finally, it enables variable expansion as described in Shell Parameter Expansion:

    The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

    When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

    The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.

    Let's see a basic example. Here, the use of ${} allows us to do something that a simple $ does not. Consider we want to write $myvar + "blabla"::

    $ myvar=23
    $ echo $myvar
    23
    $ echo $myvarblabla
                            <--- the variable $myvarblabla doesn't exist!
    $ echo ${myvar}blabla
    23blabla
    
    0 讨论(0)
提交回复
热议问题