Linux Gazette: Variable Mangling in Bash with String Operators | Linux Today

Linux Gazette: Variable Mangling in Bash with String Operators

Written By
Web Webster
Web Webster
Sep 23, 2000

“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’.

#!/bin/bash
# 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
   else
       mv $i ${i%l}
   fi
done

Complete
Story

Web Webster

Web Webster

Web Webster has more than 20 years of writing and editorial experience in the tech sector. He’s written and edited news, demand generation, user-focused, and thought leadership content for business software solutions, consumer tech, and Linux Today, he edits and writes for a portfolio of tech industry news and analysis websites including webopedia.com, and DatabaseJournal.com.

Linux Today Logo

LinuxToday is a trusted, contributor-driven news resource supporting all types of Linux users. Our thriving international community engages with us through social media and frequent content contributions aimed at solving problems ranging from personal computing to enterprise-level IT operations. LinuxToday serves as a home for a community that struggles to find comparable information elsewhere on the web.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.