A more efficient replacement for basename

Sep 02, '02 10:13:24AM

Contributed by: barrysharp

Many people use the basename program to obtain the base name of a full pathname, such as in (using the Borne Shell sytax):

Pathname=/usr/bin/archibald
Filename=`basename $FILE`
A far more efficient method, which doesn't require a new process (a very expensive OS task) nor extra memory space, is to use the powerful Shell built-in variable substitution. Thus the above can be done simply by:
Pathname=/usr/bin/archibald
Filename="${Filename##*/}"
If basename is used in many places, this can provide a significant benefit.

System performance is usually improved by making many small improvements. Rarely is there a single change that dramatically improves performance -- so my advice is to make all the small ones you can.

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020902071324228