i have seen in many script usage of "$var" and "${var}", so I just want to know diffrence between "$var" and "${var}"
1 Answer
The braces explicitly denote the beginning and end of the parameter syntax, otherwise, there is no difference. This makes certain statements unambiguous, for example:
$ foo=bar
$ echo "$fooa" # Is this $foo + a, or $fooa?
$ echo "${foo}a"
bara
-
1but i can use
echo "$foo"athen why braces in used?Rahul Patil– Rahul Patil2013-02-13 08:18:36 +00:00Commented Feb 13, 2013 at 8:18 -
2@RahulPatil Then there is no difference.
"is an illegal character in a variable name, so it terminates there.Chris Down– Chris Down2013-02-13 08:23:27 +00:00Commented Feb 13, 2013 at 8:23 -
@RahulPatil But, with the braces you can do a lot more! See also the link provided by manatwork in the comment to your question.Bernhard– Bernhard2013-02-13 08:55:48 +00:00Commented Feb 13, 2013 at 8:55
-
3There's no ambiguity in the first statement - It's clearly the value of the
fooavariable. Bash is not DWIM-compatible :)l0b0– l0b02013-02-13 09:17:10 +00:00Commented Feb 13, 2013 at 9:17 -
2@l0b0 - perhaps I should have said "ambiguous to humans", obviously the actual syntax is defined ;-)Chris Down– Chris Down2013-02-13 09:23:45 +00:00Commented Feb 13, 2013 at 9:23