Differences between revisions 4 and 6 (spanning 2 versions)
Revision 4 as of 2008-11-22 23:13:30
Size: 749
Editor: GreyCat
Comment: first-line
Revision 6 as of 2022-10-20 21:17:06
Size: 755
Editor: emanuele6
Comment: fix indentation in code blocks, add syntax highlighting
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
{{{
    for file in *.doc
    do
     base=`echo "$file" | sed 's/\.[^.]*$//'` # remove everything starting with last '.'
        mv "$file" "$base".txt
    done
{{{#!highlight sh
for file in ./*.doc
do
    base=`echo "$file" | sed 's/\.[^.]*$//'` # remove everything starting with last '.'
    mv "$file" "$base".txt
done
Line 17: Line 17:
{{{
    var=`expr "$var" : '\(.*\).'`
{{{#!highlight sh
var=`expr "$var" : '\(.*\).'`

How do I get the effects of those nifty Bash Parameter Expansions in older shells?

Most of the extended forms of parameter expansion do not work with the older BourneShell. If your code needs to be portable to that shell as well, sed and expr can often be used.

For example, to remove the filename extension part:

   1 for file in ./*.doc
   2 do
   3     base=`echo "$file" | sed 's/\.[^.]*$//'`    # remove everything starting with last '.'
   4     mv "$file" "$base".txt
   5 done

Another example, this time to remove the last character of a variable:

   1 var=`expr "$var" : '\(.*\).'`

or (using sed):

    var=`echo "$var" | sed 's/.$//'`

BashFAQ/074 (last edited 2022-10-20 21:17:06 by emanuele6)