Differences between revisions 4 and 5
Revision 4 as of 2008-11-22 23:13:30
Size: 749
Editor: GreyCat
Comment: first-line
Revision 5 as of 2009-09-15 18:06:54
Size: 751
Editor: GreyCat
Comment: ./*
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
    for file in *.doc     for file in ./*.doc

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:

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

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

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

or (using sed):

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

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