0

This question already has an answer here:

I know that ${var} and $var does the same thing. I also know that you can use brace for advanced options like ${var:=word} or ${var#pattern}. It's also explained here.

But I see in some bash scripts the use of braces in the simplest form : ${var} and sometimes I see only this notation $var.

Is there any reason to prefer this syntax over the simple $var as it does the same thing ? Is it for portability ? Is it a coding style ?

As a conscientious developer, which syntax I should use ?

marked as duplicate by Stephen Kitt, Valentin Bajrami, Flup, Gilles bash Jun 9 '16 at 11:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

It is for the sake of clarity. As pointed out by arzyfex, there is a difference between ${foo}_bar and $foo_bar. Always use braces, and you will never make that mistake. Also note that you need the braces if you wish to refer to positional parameters with more than one digit, e.g. ${11}.

  • 2
    Whether to use {...} or not is a cosmetic matter (I personally find it clutters scripts when not necessary), and when they are needed and you forget them, that's an error you would detect at authoring time. In comparison, forgetting quotes around variables is a mistake and an error you don't always detect at authoring time, but your users at run time. I find it exasperating when people go all the trouble of adding those {} when they are not necessary but forget to quote. IOW, rm ${file} is wrong, whether to use rm -- "$file" or rm -- "${file}" is a cosmetic choice. – Stéphane Chazelas Jun 9 '16 at 11:33
  • Good point about ${11} (though it's not needed in all shells (not in zsh, csh, tcsh)). Also note that ${foo}_bar can also be written "$foo"_bar or $foo''_bar or $foo\_bar (I'd still prefer "${foo}_bar"). – Stéphane Chazelas Jun 9 '16 at 11:35
  • In csh, tcsh or zsh you also need it before a : to prevent history modifiers (${var}:h as otherwise $var:h would mean the head of $var). – Stéphane Chazelas Jun 9 '16 at 11:38
  • @StéphaneChazelas Yes, I suppose I should not assume that everyone uses bash. As you say, there are various methods of indicating the end of the variable name, but I prefer curly braces. I find them more clear and more consistent than the alternatives, since they are used for various types of complex parameter expansions, including arrays. – Michael Vehrs Jun 9 '16 at 11:45
  • Thank you @MichaelVehrs for you answer. I doubt I could make the mistake to write $foo_bar instead of "$foo"_bar, so I think I will still use the simplest form $foo. Obviously, quoting variables is the most important, and it's really clear in my mind. – Nairolf21 Jun 9 '16 at 18:05
3

{} is known as brace expansion. ${} is known as variable expansion. Variables are declared and assigned without $ and without {}. You have to use

foo=50

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

$foo      // use the variable
${foo}    // same as above
${foo}bar // expand foo, and append "bar" too
$foobar   // same as ${foobar}, i.e expand a variable called foobar, 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, $foo=50 doesn't do what you might think it does!


Braces are also used to execute a sequence of commands in the current shell context, e.g.

$ { date; top -b -n1 | head ; } >logfile 
# 'date' and 'top' output are concatenated, 

$ { date; make 2>&1; date; } | tee logfile
# now we can calculate the duration of a build from the logfile

Not the answer you're looking for? Browse other questions tagged or ask your own question.