Quote
It is always better to quote parameter expansions when you want to keep the expanded value not split into several words and affected by the value of IFS. For example:
$ IFS=" elr"
$ var="Hello World"
$ printf '<%s> ' $var; echo
<H> <> <> <o> <Wo> <> <d>
$ printf '<%s> ' "$var"; echo
<Hello World>
However, there are some very limited instances that require an unquoted expansion to actually get the splitting done:
$ IFS=$' \t\n'
$ var="one two three"
$ array=($var)
$ declare -p array
declare -a array=([0]="one" [1]="two" [2]="three")
Links on the subject:
When is double-quoting necessary?
Gilles
Stéphane Chazelas
Braces
Braces are always required when the characters following the variable name should not be joined with such variable name:
$ var=one
$ echo "The value of var is $varvalue"
The value of var is
$ echo "The value of var is ${var}value"
The value of var is onevalue
From LESS="+/which is not to be interpreted as part" man bash
${parameter}
The braces are required … when parameter is followed by a character which is not to be interpreted as part of its name.
Additionally; braces are required when dealing with any double digit positional parameter.
$ set -- one two t33 f44 f55 s66 s77 e88 n99 t10 e11 t12
$ echo "$11 ${11} $12 ${12}"
one1 e11 one2 t12
Read the manual: LESS="+/enclosed in braces" man bash
When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces
Or LESS="+/with more than one digit" man bash
${parameter}
The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, …
Quotes vs Braces
when shall we use double quote around parameter expansion instead of braces around parameter name? When the other way around? When does either of the two work?
There is no rule for "shall" only the open posibility of using either:
$ var=One
$ echo "ThisIs${var}Var"
ThisIsOneVar
$ echo "ThisIs""$var""Var"
ThisIsOneVar
$ echo 'ThisIs'"$var"'Var'
ThisIsOneVar
$ echo 'ThisIs'"${var}"'Var'
ThisIsOneVar
All the expansions are entirely equivalent, use any one that you like better.
${variable_name}
doesn’t mean what you think it does … and But what if …? – G-Man Says 'Reinstate Monica' Feb 04 '18 at 03:56