Linux Today: Linux News On Internet Time.

Linux Gazette: Variable Mangling in Bash with String Operators

Sep 23, 2000, 17:41 (1 Talkback[s])
(Other stories by Pat Eyler)

"Have you ever wanted to change the names of many files at once? How about using a default value for a variable if it has no value? These and many other options are available to you through string operators in bash and other Bourne shell derived shells."

"There are three kinds of variable substitution:

  • Pattern Matching,
  • Substitution,
  • Command Substitution.
I'll talk about the first two and leave command substitution for another article."

"There are two kinds of pattern matching available, matching from the left and matching from the right. The operators, with their functions and an expample, are shown in the following table... These operators can be used to do a variety of things. For example, the following script will change the extension of all '.html' files to '.htm'. '.html' files to '.htm'.

# quickly convert html filenames for use on a dossy system
# only handles file extensions, not file names

for i in *.html; do 
   if [ -f ${i%l} ]; then
       echo ${i%l} already exists
       mv $i ${i%l}

Complete Story

Related Stories: