What is the difference between $ {varname} and $ varname in shell scripts

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

I use both, but I see no difference that could tell me when to use one or the other.

+6
source share
3 answers

Using {} in variable names helps eliminate ambiguity when performing variable expansion.

Consider the two variables var and varname . Suppose you wanted to add the name string to the var variable. You cannot say $varname , because it will result in the extension of the varname variable. However, by saying ${var}name , you can 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 the 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 

Quote from info bash :

  Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. 
+10
source

They are the same in the main case, but using ${varname} gives more control and the ability to work with a variable. He also overlooks boundary cases in which he can create confusion. And finally, it allows you to modify variables, as described in Extending Shell Settings :

The $$ character introduces a parameter extension, a substitution or arithmetic expansion command. The name or symbol of the parameter can be enclosed in braces, which are optional, but serve to protect the variable, which will be immediately expanded with the characters following it, which can be interpreted as part of the name.

When curly braces are used, a suitable ending bracket is the first '}, not escaped with a backslash or inside a quoted string, and not inside a built-in arithmetic extension, command substitution, or extension parameter.

The main form of parameter expansion is $ {parameter}. The value of the parameter is replaced. Brackets are needed when the parameter is a positional parameter with more than one digit or when the parameter is followed by a character that should not be interpreted as part of its name.

Let's look at a basic example. Here, using ${} allows us to do what simple $ does not. Consider what we want to write $myvar + "blabla" ::

 $ myvar=23 $ echo $myvar 23 $ echo $myvarblabla <--- the variable $myvarblabla doesn't exist! $ echo ${myvar}blabla 23blabla 
+9
source

The difference becomes important when something follows the variable:

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

Without parentheses, the shell will see texts as the name of a variable that will not work.

Brackets are also needed when you use the extended syntax to specify default values ​​( ${name-default} ), display errors when undefined ( ${name?error} Error ${name?error} ) or replace templates ( see this article for other templates , it is for BASH but most of the work is also for KSH)

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

on this topic:

+3
source

Source: https://habr.com/ru/post/956468/