| Date: Wed, 16 Jan 2008 18:08:57 +0000
| From: Giulivo Navigante <giulivo.navigante at katamail.com>
|
| Braun Brelin wrote:
| > I'm trying to figure out what differences there are between the following:
| > $Foo
| > ${Foo}
|
| playing with some shell scripts I discovered this some months ago:
|
| # export VAR=val
| # echo $VAR
| val
| # echo $VAR-X
| val-X
| # echo $VAR_X
|
| # echo ${VAR}_X
| val_4
|
| which seems to be helpful when you're going to use variables and
| they're a mixed or accompained by strings
that's correct. (b.t.w., the `export' is not
needed; a simple VAR=val suffices.)
there is NO semantic difference between the two
constructs; the difference is all syntactic.
${foo} is the (value of) variable `foo', ALWAYS
(when substitutions are valid); $foo is also,
except in situations like $foobar --- where what
you meant is the value of `foo' prepended to the
constant string "bar".
that can be written in several different ways,
depending on context, but the construction which
always works is ${foo}bar.
the bash(1) manual page says it very simply:
${parameter}
The value of `parameter' is substituted.
The braces are required when `parameter' [ ... ]
is followed by a character which is not to be
interpreted as part of its name.
other ways ${foo}bar can be written include:
"$foo"bar
$foo"bar"
"$foo""bar"
"$foo"'bar'
$foo'bar'
and more exotic possibilities. however, the issue
will all of the above alternatives is they break
if used within (i.e., as a part of) certain quoted
strings. the simple ${foo}bar does not break.
some people advocate always using ${foo}, claiming
it improves clarify --- Clearly Wrong since the OP
was(? is?) confused --- and/or prevents mistakes,
which whilst broadly true, also means the script's
author does not understand the shell or its syntax
(since, in practice, the problem rarely arises and
some(? many?) people find excessive un-necessary
use of the construct annoying).
cheers!
-b(ellowing )l(oudly )f(or better scripts)-
p.s. in some Bourne-ish shells, the ${...} construct
also allows accessing args > 9 (e.g., ${10}).
(the elucidated part of the above bash(1) quote
refers to this case.) older shells cannot do
this, and I don't know if it's in POSIX or not.
--
“How many surrealists does it take to | Brian Foster
change a lightbulb? Three. One calms | somewhere in south of France
the warthog, and two fill the bathtub | Stop E$$o (ExxonMobile)!
with brightly-coloured machine tools.” | http://www.stopesso.com
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!