I've written many unix scripts over the past years and very early on I found myself using the basename command numerous times.
As I'm always concerned about performance I looked into using the powerful shell built-in variable substition feature to reduce my use of basename.
Read the rest of the article to see what I came up with...
Let's say (purely an example to make my point) you invoke a Borne/Korn Shell script using
~/bin/BackupsFullBaseBackup.commandand within this script you wish to log various messages using a functions such as
function logitThis function uses basename to obtain the base of the path ~/bin/Backups/FullBaseBackup.command which is "FullBaseBackup.command".
{
echo "`basename $0`: $1"
return 0
}
NAME=="${0##*/}" # Obtain base of the calling pathnameand change function logit tofunction logitOn the face of it this may not seem much of a 'trick' or performance improvement -- but if this avoids basename being called hundreds of times each day, month after month, year after year then there's considerable benefit over the long haul.
{
echo "${NAME}: $1"
return 0
}
Mac OS X Hints
http://hints.macworld.com/article.php?story=20011226104002713