1

What is the difference between using and not using {} with the $variable in bash?

In the two statements below, which one is modern and recommended way write bash script (bash version 3.2+ above)

[~]$ variable=hello
[~]$ echo "$variable world"
hello world
[~]$ echo "${variable} world"
hello world
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 1
    100% equivalent. There's no universally accepted convention; which to prefer is a matter of opinion (for my own opinion, I strongly prefer `$foo` over `${foo}` in places where it doesn't matter). – Charles Duffy Dec 29 '15 at 18:54
  • 1
    ...the places where it *does* matter are those where you're parameterizing the expansion, or concatenating it with a constant string whose first character is a legal part of a variable name. – Charles Duffy Dec 29 '15 at 18:55
  • 4
    The difference becomes relevant when you have `"$variable_world"` vs `"${variable}_world"`. – Benjamin W. Dec 29 '15 at 18:55
  • @CharlesDuffy Thank you! – Senthil Kumaran Dec 29 '15 at 19:36

0 Answers0