Differences between revisions 2 and 3
Revision 2 as of 2008-05-22 18:08:04
Size: 750
Editor: GreyCat
Comment: clean up slightly
Revision 3 as of 2008-11-22 14:09:09
Size: 751
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq74)]] <<Anchor(faq74)>>
Line 4: Line 4:
Most of the extended forms of [:BashFAQ/073: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. Most of the extended forms of [[BashFAQ/073|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.

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)