The main reason for the curly braces is to distinguish the variable name
from the rest of the sentence.
Ugh, now I have to think of an example.
Let's say you were renaming a file. Maybe the existing file's name is
"input.dat" and after processing the input, you want to archive it by
date... so you want to rename "input.dat" to "20090511input.dat" Just
for an example. mv is the command to rename.
So ... if you tried to use "simple" variable replacement you'd have this
(won't work):
curdate=$(date +%Y%m%d)
mv input.dat $curdateinput.dat
The problem is that the system won't insert $curdate in front of
"input". Why not? Because it's looking for a variable named
$curdateinput instead of a variable named $curdate. ooops.
So you'd do this instead:
curdate=$(date +%Y%m%d)
mv input.dat ${curdate}input.dat
Now it knows that $curdate is the name of the variable, and "input" is
just plain text that's part of the filename.
But... curly braces can do lots of other stuff, too. For example, it
can be used to do a find & replace. Maybe you want to rename all files
named .DAT with .CSV...
for file in *.DAT; do
mv $file ${file/.DAT/.CSV}
done
this runs a loop, and each time through sets $file to the name of one
file matching the pattern *.DAT. The ${file/.DAT/.CSV} searches for
.DAT in the string and replaces it with .CSV so you can easily keep the
rest of the filename the same, but change the .DAT to .CSV.
There are many other things that can be done with curly braces.. I won't
try to list them all, instead see the docs in the info center:
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/rzahz/paramexp.htm
rob@xxxxxxxxx wrote:
When would it need the non invariant braces?
As an Amazon Associate we earn from qualifying purchases.