× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




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.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.